石头,布,剪刀与机器人,为什么我的程序不打印出任何答案,即使我定义机器人移动使用"s"?



在我的代码中,我做了很多if语句,我知道这是很多,但这是我能想到的唯一方法,但它首先说Botmove未定义,然后我添加了" s ",但现在它没有打印任何东西,例如,我做了y,然后r,但没有出现。有人能帮我吗?这是我的代码:


random=random.randint(1,3)
if random==1:
botmove='r'
if random==2:
botmove='p'
if random==3:
botmove='s'

print('Play Rock,Paper,Scissors?')
print('Y/N')
play=input('>').lower().strip()
if play=='n':
print('Thank You For Playing.')
if play=='y':
print('Rock Or Paper Or Scissors?')
print('R/P/S')
move=input('>').strip().lower()

#code
if move=='r':
if botmove=='r':
print('The Computer Chose:',botmove)
print('Draw.')
if botmove=='p':
print('The Computer Chose:',botmove)
print('You Lost.')
if botmove=='s':
print('The Computer Chose:',botmove)
print('You Won.')
if move=='p':
if botmove=='r':
print('The Computer Chose:',botmove)
print('You Won.')
if botmove=='p':
print('The Computer Chose:',botmove)
print('Draw.')
if botmove=='s':
print('The Computer Chose:',botmove)
print('You Lost.')
if move=='s':
if botmove=='r':
print('The Computer Chose:',botmove)
print('You Lost.')
if botmove=='p':
print('The Computer Chose:',botmove)
print('You Won.')
if botmove=='s':
print('The Computer Chose:',botmove)
print('Draw.')

编辑:谢谢大家的帮助!

因为botmove是一个变量,你不应该把它放在引号里,因为这会把它变成一个字符串。

此外,当将r分配给botmove时,botmove == 'r'是不正确的。因为==是一个比较运算符。所以你必须将其替换为=botmove变量赋值给r

,你可以替换:

if move=='r':
if botmove =='p':
print('You Lost.')
if move=='r':
if botmove =='r':
print('Draw.')
if move=='r':
if botmove =='s':
print('You Won.')

:

if move=='r':
if botmove =='p':
print('You Lost.')
if botmove =='r':
print('Draw.')
if botmove =='s':
print('You Won.')

同样,您可以使用random.randint而不是random.randrange来返回一个整数。但是,您应该传递(1,3)而不是(1,4)

那么,最后你的代码应该看起来像这样:
import random
random=random.randint(1,3)
if random==1:
botmove ='r'
if random==2:
botmove ='p'
if random==3:
botmove ='s'

print('Play Rock,Paper,Scissors?')
print('Y/N')
play=input('>').lower().strip()
if play=='n':
print('Thank You For Playing.')
if play=='y':
print('Rock Or Paper Or Scissors?')
print('R/P/S')
move=input('>').strip().lower()
if move=='r':
if botmove=='p':
print('You Lost.')
if botmove=='r':
print('Draw.')
if botmove=='s':
print('You Won.')
‘’’

相关内容

最新更新