2013-07-27 42 views
-3
import random 
print ("welcome to my game") 

guessestaken = 0 
number = random.randint(1,10) 

print ("what is your name") 

myname = raw_input() 

print ("Well,"+ myname + "," + "I'm thinking of a number between 1 and 10" 

while guessesTaken < 6: 
    print ("take a guess") 
    guess = raw_input() 
    guess = int(guess) 
    guesstaken = guesstaken + 1 

    if guess < number : 
     print ("your guess is low") 
    if guess > number : 
     print ("your guess is high") 
    if guess == number : 
     break 
if guess == number : 
    print ("You are awesome ") 
    guesstaken = str(guessestaken) 
    print ("You guessed the number in " + guessestaken + " try") 

if guess != number: 
    print "You lost" 

每次我試圖執行它的錯誤「而guessesTaken < 6: ^ 語法錯誤:無效的語法」的while語句之前出現什麼是錯,此代碼猜測遊戲蟒蛇

+1

你錯過拼寫gusestaken(首都)後「1至10」 – AdamKG

+0

缺少一個右括號 – elyashiv

+1

這個問題似乎是題外話,因爲它是由一個錯字造成的問題。 – madth3

回答

1

falsetru已經回答了你的問題。您還可以在guessesTaken,guesstakenguessestaken之間自由切換。我只是想補充一點,python有一個很好的控制語句來確定一個循環是否已經完成,因爲它已經被完全或者破壞了(由break)。在這些情況下,您可以使用while...else

在你的代碼,它是這樣的:

#! /usr/bin/python3 

import random 

number = random.randint(1, 10) 
print ('Welcome to my game.') 

myName = input ('What is your name? ') 
print ('Well, {}, I\'m thinking of a number between 1 and 10'.format (myName)) 

guessesTaken = 0 
while guessesTaken < 6: 
    guess = int (input ('Take a guess: ')) 
    guessesTaken += 1 

    if guess < number: print ('Your guess is low.') 
    if guess > number: print ('Your guess is high.') 
    if guess == number: 
     print ('You guessed the number in {} tr{}.'.format (guessesTaken, 'y' if guessesTaken == 1 else 'ies')) 
     break 
else: print ('You lost.') 
2

print聲明省略右括號:

print ("Well,"+ myname + "," + "I'm thinking of a number between 1 and 10" 

刪除左括號或附加右括號。

+0

它的工作感謝 – user152178

+0

@ user152178,[接受答案](http://stackoverflow.com/help/accepted-answer)如果我的回答是有幫助的。 – falsetru