Every time I put in a number, it only prints the first input everytime instead of it being random. This is the code I used:
import random
import sys
answer = True
while answer:
question = input("Roll the di to get your fortune: ")
answers = random.randint(1,6)
if question == "":
sys.exit()
elif answer == 1:
print("You will get a new friend. ")
elif answer == 2:
print("You will go to disneyland. ")
elif answer == 3:
print("You will receive kindess.. ")
elif answer == 4:
print("Your will get a dog. ")
elif answer == 5:
print("You will suffer. ")
elif answer == 6:
print("You will have bad luck. ")
roll()
I really need help, I cant seem to figure out what I am doing wrong.
CodePudding user response:
You have a typo here where you're assigning to answers
instead of answer
:
answers = random.randint(1,6)
The True
value you assigned to answer
is the only one it will ever have; True
is considered equal to 1
which is why you only ever get the first answer.
Using answer
as your loop condition as well as for the die roll is a little confusing -- once you fix the typo above, it kind of works because all of the die rolls are "truthy", but it would work just as well (and be less prone to causing mistakes like this one) to use while True
for the loop rather than using a variable whose value can never be false. Note that if you hadn't set answer = True
at the start of your script, you would have spotted the answers
typo immediately because checking answer == 1
would have raised a NameError
. In general, the fewer variables you have to keep track of, and the less time you have to keep track of them for, the fewer chances there are for something to go wrong!
You don't need the question
variable either since you immediately break the loop if it's empty and don't do anything else with it -- and for that matter you don't really need to keep track of answer
if you immediately use it to look up the thing to print and then continue on with the next loop iteration.
import random
while True:
if not input("Roll the die to get your fortune: "):
break
print({
1: "You will get a new friend. ",
2: "You will go to disneyland. ",
3: "You will receive kindess.. ",
4: "Your will get a dog. ",
5: "You will suffer. ",
6: "You will have bad luck. ",
}[random.randint(1, 6)])
Roll the die to get your fortune: ok
You will suffer.
Roll the die to get your fortune: ok
You will receive kindess..
Roll the die to get your fortune: ok
You will suffer.
Roll the die to get your fortune: ok
You will receive kindess..
Roll the die to get your fortune: ok
Your will get a dog.
Roll the die to get your fortune: ok
You will have bad luck.
Roll the die to get your fortune: ok
You will go to disneyland.
Roll the die to get your fortune:
CodePudding user response:
You have an extra s when you roll the die on answers. It should be:
answer = random.randint(1,6)
CodePudding user response:
As a side note, you could make your code easier to work with if you replace your elifs with a dictionary of responses:
import random
import sys
responses = {
1: "You will get a new friend. ",
2: "You will go to disneyland. ",
3: "You will receive kindess.. ",
4: "Your will get a dog. ",
5: "You will suffer. ",
6: "You will have bad luck. "
}
answer = True
while answer:
question = input("Roll the di to get your fortune: ")
answer = random.randint(1,6)
if question == "":
sys.exit()
else:
print(responses[answer])
roll()
CodePudding user response:
Change these lines in your code :
elif answer == 1:
print("You will get a new friend. ")
elif answer == 2:
print("You will go to disneyland. ")
elif answer == 3:
print("You will receive kindess.. ")
elif answer == 4:
print("Your will get a dog. ")
elif answer == 5:
print("You will suffer. ")
elif answer == 6:
print("You will have bad luck. ")
to these:
elif answers == 1:
print("You will get a new friend. ")
elif answers == 2:
print("You will go to disneyland. ")
elif answers == 3:
print("You will receive kindess.. ")
elif answers == 4:
print("Your will get a dog. ")
elif answers == 5:
print("You will suffer. ")
elif answers == 6:
print("You will have bad luck. ")
Where you went wrong: You were using answer
variable defined as True
in your conditions. In Python, True
has a value of 1
this is why it was always running the condition "You will get a new friend. "
. Also, you were storing the random dice digits in answers
variable. Hope you understood!