2017-06-05 276 views
-6

所以我在這個項目擁有先進的,我有以下代碼Python的隨機數字猜謎遊戲

import random 
number = random.randint(1,100) 

name = input('Hi, Whats your name?') 
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess") 


guess1 = input() 
if guess1 == number: 
    print ("Good job, you got it!") 
while guess1 != number: 
    if guess1 > number: 
     print ('your guess is too high') 
    if guess1 < number: 
     print ('your guess is too low') 

它throwns是>或無法海峽之間使用<和int

錯誤怎麼會有我替換符號,以便劑量觸發該錯誤?

+1

您所要求的分配的核心。你有什麼嘗試,你失敗了? – Uriel

+0

作爲註釋,您最好在輸入內包含輸入文本:'myName = input('Hello!你叫什麼名字?')'以防止print print語句後面的換行符。 – Uriel

回答

-1

您可以在while循環中的條件,以檢查其是否正確。它可以通過以下方式完成。

import random 

print('Hello! What is your name?') 
myName = input() 

number = random.randint(1, 100) 
print('Well, ' + myName + ', I am thinking of a number between 1 and 100.') 
inputNumber = int(raw_input('Enter the number') 
while inputNumber != number: 
     print "Sorry wrong number , try again" 
     inputNumber = int(raw_input('Enter the number') 
print "Congrats!" 
0

我希望這對你的作品:

import random 
myname = input('Hello, what is your name?') 
print('Well',myname,'am thinking of a number between 1 and 100') 
number = random.randint(1,100) 
guess = 0 
while guess < 4: 
    guess_number = int(input('Enter a number:')) 
    guess += 1 
    if guess_number < number: 
     print('Your guess is to low') 
    if guess_number > number: 
     print('Your guess is to high') 
    if guess_number == number: 
     print('Your guess is correct the number is',number) 
     break 
    if guess == 4: 
     break 
print('The number i was thinking of is',number) 
2

有在你的代碼的兩個錯誤。

  1. 您需要將guess1的輸入從字符串(默認情況下)轉換爲整數,然後才能將其與數字(整數)進行比較。

  2. while循環永遠不會停止,因爲您不讓用戶輸入另一個值。

試試這個:

import random 
number = random.randint(1,100) 

name = input('Hi, Whats your name?') 
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess") 

guess1 = int(input()) # convert input from string to integer 

while guess1 != number: 
    if guess1 > number: 
     print ('your guess is too high. Try again.') 
    elif guess1 < number: 
     print ('your guess is too low. Try again.') 

    guess1 = int(input()) # asks user to take another guess 

print("Good job, you got it!") 
-2
from random import * 

def play_game(): 
    print("Let's play a number guessing game") 
    # Selecting a random number between 1 and 100 
    number = randint(1, 100) 
    choice = int(input("I am thinking of a number between 1 and 100. Could you guess what it is? ")) 

    # Guide the user towards the right guess 
    # Loop will continue until user guesses the right number 
    while choice != number: 
     if choice < number: 
      choice = int(input("Too low. Can you try again? ")) 
     elif choice > number: 
      choice = int(input("Too high. Can you try again? ")) 
    continue_game = input("You guessed it right! Would you like to play it again? (Y/N) ") 

    # Restart the game if user wishes to play again 
    if continue_game == "Y": 
     print("--" * 42) 
     play_game() 
    else: 
     print("Thanks for playing :)") 
     exit(0) 

play_game()