2017-09-25 83 views
2

這裏是我的代碼如何使Python中的猜謎遊戲,我掙扎

import random 
print ('hello') 
print ('my name is IB what is your name') 
mn = input() 
print ('it is good to meet you ' + mn+ ' I am thinking of a nomber 1 to 
100') 
it = 1 
jojm = random.randint (1, 100) 
print ('guess') 
guess =input() 
while it < 100: 
    if guess == jojm: 
     print ('game over ') 
     break 
    elif guess < jojm: #it keeps saying the problem is here 
     print ('to low') 
     print ('guess') 
    elif guess > jojm: 
     print ('to high') 
     print ('guess') 
    it = it + 1 
print =it 

我不知道該怎麼辦,請幫助我需要它,如果有人可以幫助請不要

+1

有沒有真正的理由循環100次。用戶可能猜兩次相同的號碼或猜測1-100範圍以外的號碼。你可能會改善,只是做一個無限循環。 「雖然是真的:」當他們做對了的時候就休息了。你可以按ctrl + c來逃避循環。 –

回答

0

把這個線

guess =input() 

在while循環 不是每次猜測是打印程序會要求你輸入一個值。

1

你應該輸入迴路中,也解析它作爲一個整數:

while it < 100: 
    guess = int(input()) 
    #rest of your code 

TIP:你可以提供輸入法本身的提示文本像input('Guess ?')

+0

雖然在猜謎遊戲中比僅僅一段時間更合適。 – chrisckwong821

+1

@ chrisckwong821 python沒有做什麼,你需要自己模擬。 –

+0

你是對的,我來自其他語言,並沒有意識到這一點。 – chrisckwong821

1

轉換guess =input()guess =int(input())

原因guess =input()需要string並且您正在與int進行比較。

在這裏你可以試試這個。

jojm = random.randint (1, 100) 
print('guess') 
while True: 
    guess = int(input()) 
    if guess == jojm: 
     print ('game over ') 
     break 
    elif guess < jojm: 
     print ('to low') 
     print ('guess') 
    elif guess > jojm: 
     print ('to high') 
     print ('guess') 
1

的問題是,guess是一個字符串,你需要把它隱蔽爲int:

import random 
print ('hello') 
print ('my name is IB what is your name') 
mn = input() 
print ('it is good to meet you %s I am thinking of a number between 1 and 
100' % mn) 
it = 1 
jojm = random.randint (1, 100) 
print ('guess') 
getGuess = input() 
guess = int(getGuess) #convert it first to an integer 
while it < 100: 
    if guess == jojm: 
     print ('game over ') 
    elif guess < jojm: 
     print ('to low') 
     print ('guess') 
    elif guess > jojm:  
     print ('guess') 
     it = it + 1 

print (it) 
+0

而且你的代碼還有其他問題,但這應該工作正常 –

+0

這不起作用 – jojm

+0

@jojm它適用於我完全。只要刪除第5行和第6行之間它說100的空間。我必須包括空間,因爲它是在一個答案。 –