游戏代码错误行跳 - Python

问题描述:

我有一个猜数字游戏的代码,当我发现问题时我正在玩它。游戏代码错误行跳 - Python

#猜数

print("I'm thinking of a number between 1 and 20. Can you guess my number?") 

r = str(input('{} please enter a integer between 1 and 10: '.format(name))) 

r = str(input('{} please enter a integer between 1 and 10: '.format(name2))) 

print (name, ' you chose ', r) 
print (name2, ' you chose ', p) 
r1 = random.randrange(10) 

打印谁赢得

if r == p: 
    print('Plz choose different numbers from each other') 
if r == r1: 
    print('Computer chose', r1,',',name,' Wins!') 
if p == r1: 
    print('Computer chose', r1,',',name2,' Wins!') 
elif (r > 'r1' and p < 'r1'): 
    print('Computer chose', r1, ' both of you lose') 
elif (r < 'r1' and p < 'r1'): 
    print('Computer chose', r1, ' both of you lose') 
elif (r > 'r1' and p > 'r1'): 
    print('Computer chose', r1, ' both of you lose') 
elif (r < 'r1' and p > 'r1'): 
    print('Computer chose', r1, ' both of you lose') 

当计算机读取的代码

if r == p: 
    print('Plz choose different numbers from each other') 
if r == r1: 
    print('Computer chose', r1,',',name,' Wins!') 
if p == r1: 
    print('Computer chose', r1,',',name2,' Wins!') 
elif (r > 'r1' and p < 'r1'): 
    print('Computer chose', r1, ' both of you lose') 
elif (r < 'r1' and p < 'r1'): 
    print('Computer chose', r1, ' both of you lose') 
elif (r > 'r1' and p > 'r1'): 
    print('Computer chose', r1, ' both of you lose') 
elif (r < 'r1' and p > 'r1'): 
    print('Computer chose', r1, ' both of you lose') 

的这一部分它跳过了第一部分,并一直说双方球员都输了。为什么会发生这种情况,我该如何解决?

我认为这个问题是在这里:

r = str(input('{} please enter a integer between 1 and 10: '.format(name))) 

r = str(input('{} please enter a integer between 1 and 10: '.format(name2))) 

不要你的意思是通过分配给p第二次?

elif (r > 'r1' and p < 'r1'): 
    print('Computer chose', r1, ' both of you loose') 
elif (r < 'r1' and p < 'r1'): 
    print('Computer chose', r1, ' both of you loose') 
elif (r > 'r1' and p > 'r1'): 
    print('Computer chose', r1, ' both of you loose') 
elif (r < 'r1' and p > 'r1'): 
    print('Computer chose', r1, ' both of you loose') 

你比较rp字符串'r1'p = str(...

而且,这些线没有太大的意义。你可能意味着要比较r1本身,但即使如此,逻辑真的很奇怪。我觉得你只是想if r != r1 and p != r1,但低于简单得多:( “输” 但是请注意,这个词的拼写)

if r == p: 
    print('Plz choose different numbers from each other') 
elif r == r1: 
    print('Computer chose', r1,',',name,' Wins!') 
elif p == r1: 
    print('Computer chose', r1,',',name2,' Wins!') 
else: 
    print('Computer chose', r1, ' both of you loose') 

UPDATE

你可能想整数:

r = int(input('{} please enter a integer between 1 and 10: '.format(name))) 

r = int(input('{} please enter a integer between 1 and 10: '.format(name2))) 
+0

我觉得有很多问题。下面是另外一个问题:为什么我们要比较第一个变量'p','r'和'r1'('p == r1'),然后有时会串入'r''r1'和p>'r1'? –

+0

@ Two-BitAlchemist是的,其实只是做了一个编辑。 – smarx

+0

“这个词实际上拼写为'输'。” +1 –