我刚刚开始用Python编程。我目前正在尝试制作石头,纸,剪刀的游戏。要求用户选择一个代表三个选项之一的数字。而个人电脑会选择一个随机数。但是,我得到输入线的值错误。同一行在不同的上下文中可以正常工作(没有while循环),但是我看不到自己做错了什么。任何帮助将不胜感激。
我试图将字符串转换为浮点数,然后转换为整数。那没有用。此外,我已经将玩家输入数字y替换为随机数以测试其余代码。这工作得很好。
这是我收到的错误消息:
answerplayer = int (input('What is your choice? ')) #Error
ValueError: invalid literal for int() with base 10: "runfile
代码:
import random
win = False
while win == False:
print ('Rock, Paper, Scissors. 0: Rock; 1: Scissors; 2: Paper')
print ('Make your choice')
answerplayer = int (input('What is your choice? ')) #Error
answer = random.randrange (3)
print (answerplayer)
print (answer)
if answer == 0 and answerplayer == 0 :
print ('TIE')
elif answer == 0 and answerplayer == 1 :
print ('PC Win')
win = True
elif answer == 0 and answerplayer == 2 :
print ('Player Win')
win = True
elif answer == 1 and answerplayer == 0 :
print ('Player Win')
win = True
elif answer == 1 and answerplayer == 1 :
print ('TIE')
elif answer == 1 and answerplayer == 2 :
print ('PC Win')
win = True
elif answer == 2 and answerplayer == 0 :
print ('Player Win')
win = True
elif answer == 2 and answerplayer == 1 :
print ('PC Win')
win = True
elif answer == 2 and answerplayer == 2 :
print ('TIE')
else:
print ('Player Win')
win = True
print ('done')
答案 0 :(得分:1)
代码很好。用户输入必须是int()
可以转换为整数的内容。
通行证:1
2
3.444
失败:sqfe
zero
答案 1 :(得分:1)
您可以解决:
while win == False:
print ('Rock, Paper, Scissors. 0: Rock; 1: Scissors; 2: Paper')
print ('Make your choice')
answerplayer = input('What is your choice? ')
if not answerplayer.isnumeric() :
print ('No number')
break
answerplayer = int(answerplayer)
answer = random.randrange (3)
print (answerplayer)
print (answer)