2016-08-04 45 views
-5

我有一個猜數字遊戲的代碼,當我發現問題時我正在玩它。遊戲代碼錯誤行跳 - 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') 

的這一部分它跳過了第一部分,並一直說雙方球員都輸了。爲什麼會發生這種情況,我該如何解決?

回答

2

我認爲這個問題是在這裏:

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 –