Reputation: 303
When I type play
, a random number is assigned to number1
. It asks me for a prediction and I put in a number, say 5. After putting in 5 I always get the else
statement and not the if
statement. I even put a print()
to find out what number was generated. Sometimes I'm right on or within 1 (The game also allows for within 1) and it still re-directs me to the else
statement. Could anyone help? Thanks.
money = 1000000
def luckyrollgame():
global money
from random import choice
print('You are in the game lobby of Lucky Roll.')
print('Choose either \'rules,\' \'play,\' or \'back\'')
lobby = input()
if lobby == 'rules':
luckyrollgamerules()
if lobby == 'play':
die = [1, 2, 3, 4, 5, 6]
number1 = choice(die)
prediction = input('Please type your prediction number: ')
if prediction == number1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
if prediction == number1 - 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
if prediction == number1 + 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
else:
print('I\'m sorry. You didn\'t get the number right.')
print('The number was ' + str(number1) + '.')
money = money - 1
print('You now have ' + str(money) + 'dollars.')
print('--------------------------------------------------')
altluckyrollgame()
if lobby == 'back':
altvillagescene()
else:
print('Please type a valid option.')
print('--------------------------------')
altluckyrollgame()
*Functions such as altluckyrollgame()
or altvillagescene()
are part of the game logic and defined elsewhere, so you can ignore them.
Upvotes: 0
Views: 104
Reputation: 8610
The prediction
returned by input()
is a string, so all the comparisons fail. Try casting the value to an integer:
prediction = int(input())
Upvotes: 0
Reputation: 785
Your have three different if structures there. You almost certainly want
if ...
elif ...
elif ...
else
Upvotes: 0
Reputation: 402
Use the elif
statement after the very first statement. At current, your code
if lobby == 'back':
altvillagescene()
else:
print('Please type a valid option.')
print('--------------------------------')
altluckyrollgame()
is checking if lobby == 'back' and running the else in all other cases. You probably don't want this, as the code under else is run in addition to every other if case.
if x == 0: pass
elif x == 1: pass
else: pass
Code should look like this
money = 1000000
def luckyrollgame():
global money
from random import choice
print('You are in the game lobby of Lucky Roll.')
print('Choose either \'rules,\' \'play,\' or \'back\'')
lobby = input()
if lobby == 'rules':
luckyrollgamerules()
elif lobby == 'play':
die = [1, 2, 3, 4, 5, 6]
number1 = choice(die)
prediction = input('Please type your prediction number: ')
######################### This too
try: prediction = int(prediction)
except ValueError: prediction = -10
#########################
if prediction == number1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
elif prediction == number1 - 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
elif prediction == number1 + 1:
print('Good job! You guessed right!')
money = money + 3
print('You now have ' + str(dollars) + 'dollars.')
else:
print('I\'m sorry. You didn\'t get the number right.')
print('The number was ' + str(number1) + '.')
money = money - 1
print('You now have ' + str(money) + 'dollars.')
print('--------------------------------------------------')
altluckyrollgame()
elif lobby == 'back':
altvillagescene()
else:
print('Please type a valid option.')
print('--------------------------------')
altluckyrollgame()
Upvotes: 1
Reputation: 3776
The "else" block is only matched with the final "if prediction == number1 + 1". This means that if the correct number is guessed (or the number1 - 1 case) then it still also run the final else block.
You need to change the code to use "elif" for the intermediate conditionals:
if prediction == number1:
pass # do the win
elif prediction == number1 - 1
pass # do the win
elif prediction == number1 + 1
pass # do the win
else:
pass # do the lose
Upvotes: 0
Reputation: 12287
Your problem is that you're comparing a string to an integer.
You'll need to first convert the input to an int
:
try:
guess = int(prediction)
except ValueError:
#Handle when a person enters an invalid number here
Upvotes: 1