2017-01-30 6 views
-3

私は練習のための簡単な推測ゲームを作った。プログラムはエラーなしで機能していますが、指定された出力が間違った値です。ここで 出力数が正しくありません

はコードです:

import random 

welcome_phrase = "Hi there. What's your name?" 
print("{:s}".format(welcome_phrase)) 

user_name = input("Name: ") 
print("Hey {:s}, I am Crash. Let's play a game. I am thinking of a number between 1 and 20. Can you guess the number?".format(user_name)) 

attempts = 5 
secret_num = random.randint(1,20) 

for attempt in range (attempts): 
    guess = int(input("Guess the number: ")) 
    if guess > secret_num: 
     print("Your guess is higher than the number. Try again") 
    elif guess < secret_num: 
     print("Your guess is lower than the number. Try again.") 
    else: 
     print("Well done! {:d} is the right number.".format(guess)) 
     print("It took you {:d} attempts.".format(attempt)) 
     break 

if guess != secret_num: 
    print("Sorry, you have used up all your chances.") 
    print("The number was {:d}".format(secret_num)) 

そして、ここで出力されます:あなたは3つの試みがなされたことは明らかであるにもかかわらず、上記の画像で見ることができるように

This is what the console prints back.

正しい数を推測するために、Pythonは2回しかカウントしませんでした。誰も私にこれを解決する方法を教えてもらえますか?

+1

コピーしてここにコンソール出力を貼り付けてください。また、Pythonは** 0インデックスの**言語なので、試行0,1&2は合計で –

+3

の 'range'が3回になることに注意してください。 – Matthias

+0

whileループとbreak文を使用する方が良いでしょう –

答えて

0

あなたは範囲が0

+0

助けてくれてありがとう。プログラムは正常に動作します。 –

0

から始まるようbreak句とwhileループが推測数のために非常にきれいで、これを解決するために

for attempt in range (1,attempts+1): 

for attempt in range (attempts): 

を変更することができますそれははるかに信頼性の高いゲームです。

そうもコードは3回カウンタ出力を実行し、そのわずか2((0,1,2))を言うだろう、別の時点で開始するように指定されない限りforループが0からカウントを開始

これはゲームを推測するナンバーワンの基本的なバージョンです。あなたが始めたときに作成したものですが、余分なレベルやヒントなどの他の機能を簡単に行うことができ、以下のコードを理解できない場合は教えてください。

例:

import random 

welcome_phrase = "Hi there. What's your name?" 
print("{:s}".format(welcome_phrase)) 

user_name = input("Name: ") 
print("Hey {:s}, I am Crash. Let's play a game. I am thinking of a number between 1 and 20. Can you guess the number?".format(user_name)) 

totalAttempts = 5 
secret_num = random.randint(1,20) 
usedAttempts=0 

while usedAttempts!=totalAttempts: #will run until they run out of attempts 
    guess=input("What is your guess? ") 
    if guess > secret_num: 
     print("Your guess is higher than the number. Try again") 
    elif guess < secret_num: 
     print("Your guess is lower than the number. Try again.") 
    elif guess==secret_num: #checks if they are right 
     print("Well done, You have guessed my number!") 
     print("It took you {:s} attempts".format(usedAttempts)) 
     wonGame=True 
     break #exits while loop 
    else: 
     print("Sorry that is not my number, you have lost a life. :(") 
     usedAttempts+=1 #adds 1 to the value of the used attempts 


if wonGame==True: 
    print("Sorry, you have used up all your chances.") 
    print("The number was {:d}".format(secret_num)) 
+0

助けてくれてありがとう。残念ながら、14行目にエラーがあるので、上記のコードは動作しません: 'guess = input("あなたの推測は? ")' 代わりに、 'guess = int(input("あなたの推測は? ")' –

+0

同様に、 'usedAttempts'は文字列ではなく整数値を持つので、21行目も同様にエラーが発生しますので、21行目になります:' print( "{d}回の試み" .format(usedAttempts)) ' –

+0

あなたが 'int(入力("あなたの推測は何ですか? "))を実行するとクラッシュし、後で数値に変換する必要があります – WhatsThePoint

関連する問題