While the other answers are supperior in terms of raw code, I get the feeling you are only starting out with Python. So ill try and make this as approachable as possible. For starters, I think you may be getting tripped up on what functions actually are.
input
is a function, not a value. (If you ever studied maths, think of it like f(x)
or sin(x)
/cos(x)
/tan(x)
). All the functions do are let you pass in a number, and get a different number back out. While a little more complex, input
is no different.
While this video is fairly degrading to watch, it gets the point across: https://www.youtube.com/watch?v=ZEsCla92mek
Thinking about the function as a machine (put raw materials into it, the machine changes it, and then spits something useful out), the statement if input=="yes"
is asking the computer if the machine is equal to "yes"
, and not whether the output of the machine is equal to "yes"
.
In terms of solving the error in your code, you may want to learn about while
loops. This keyword tells a computer to keep doing the same action until a function is met. For example (using some of your own variables), while (userdice1 != 1): userdice1 = random.randint(1,6)
won't stop generating a new number until the condition is met, i.e., until userdice1 == 1
.
In addition, you should have a search online about the break
keyword, which lets you escape from a loop; there are plenty of resources out there.
If you can handle those 3 points, you will be easily able to tackle this problem