English 中文(简体)
我如何将函数转换成整数?
原标题:How do I convert a function to an integer?
  • 时间:2024-07-08 01:52:53
  •  标签:
  • python
  • dice
I can t find a way to get my dice simulator to convert a function to a string to an integer. I ve been trying to incorporate a how many sides plus how many rolls feature, and I can t figure it out. import random random.seed(12039810239874610293) a = 1 b = input( How many sides would you like your die to have? ) r = input( How many times would you like to roll it? ) r = int(r) r2 = r + 1 b = int(b) def n(): for counter in range(1, r2): random.randint(1, b) return n2 n2_int = int(n2) print( You rolled a , n2_int) Although to be fair, I am a relative noob to python, having put it down for about 2 years. I ve used ChatGPT to solve it, but it hasn t helped, and have spent a while on stack overflow trying to fix it. Still displays variants of TypeError: Int() argument must be a string and ValueError: Invalid literal for int() with base 10: None EDIT: Put in full code for clarity
问题回答
this is a demonstration of how to do the said scenario first is to get the how many sides and times and convert them into int before storing in a variable import random sides = int(input( How many sides would you like your die to have? )) times = int(input( How many times would you like to roll it? )) # the declaration above is stated in a simpler way as # sides = input( string here ) # sides_int = int(sides) now let s create a function with parameters in which the function will use to properly do the instructions you want def roll_dice(sides, times): for roller in range(0, times):#this will determine how many rolls rolled = random.randint(1, sides) #this roll the dice based on sides print(rolled) #prints the rolled number now lets call the function roll_dice(sides, times) now to answer the question to pass the value to a variable, then you must have a storage for it since you want to successfully pass the value to a variable, you must do it properly, as per your request to transfer it as int to a variable, here s how you can do it, you can notice that we placed the number of times outside, since we specified that rolling the dice is a function and how many times it must be rolled is equivalent on how many times should we call the function def pass_the_dice(sides): #this is your function that only requires sides return random.randint(1, sides) for counter in range(0, times): rolled_value = pass_the_dice(sides) print(f"You rolled :{rolled_value}") I hope I made it clearer for you
your code snippet is missing data. Where does r2 come from? you are not passing it to your function as an argument: if you want the function to use a value then you have to pass a value to it. ex: def sum_two_numbers(a, b): result = a + b print(result) then you have to CALL the function.. meaning.. tell python use this function and change a , b with real values.. sum_two_numbers(2,5) this will indeed return 7 It looks like what you are trying to achieve is something like this import random rolls = int(input("How many rolls?")) #create an int variable for rolls sides = int(input("How many sides?")) #create an int variable for die sides def n(r2, b): #define positional arguments to your function for x in range(1, r2): #use your args in function print(random.randint(1, b)) #print the random result of your die n(rolls, sides) #your function call passing your previously created values print(f You rolled a {sides} sides die {rolls} times ) #fancy report




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签