So a while ago when I first learned Python I made a simple dice roll or coin flip project. I had problems with the if statements not correctly portraying the information, and I didn't think too much about it. Well now Im on a new project and am having the same issues so I want to know what is wrong.
Here is my code for the original project
import random
def game():
print('welcome to randomizer \n, you can roll a 20 sided dice \n'
', or you can flip a coin which still may be buggy \n, '
'in case it is buggy just read the number above the text 1 = heads and 2 = tails.')
def roll_dice(num):
return random.randint(1, num)
def coin_flip():
return random.randint(1, 2)
hi = str(input('roll dice or coin flip: '))
if hi == 'roll dice':
print(roll_dice(20))
re = input('press enter to close or r to restart: ')
if re == 'r':
game()
else:
print('Bye')
elif hi == 'coin flip':
print(coin_flip())
if coin_flip() == 1:
print('wow you got heads = 1')
bye = input('press enter to close or r to restart')
if bye == 'r':
game()
else:
print('Bye')
elif coin_flip() == 2:
print('wow you got tails = 2')
bye = input('press enter to close or r to restart')
if bye == 'r':
game()
else:
print('Bye')
else:
print('there was an error')
game()
The game is simple, you choose which one you want to do and it gives you the result. For some reason there were inconsistencies in the number and what that was. It would like print on one line 1 then on the next line say you got tails which is 2 (I added where it says which is number to find out what was happening). I would like to know what is wrong because I want to learn and get better at Python, so I can get a job later down the line and become a python master.
Aucun commentaire:
Enregistrer un commentaire