Python的随机数字猜谜游戏

问题描述:

所以我在这个项目拥有先进的,我有以下代码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

您可以使用一个循环在这里 - https://www.tutorialspoint.com/python/python_while_loop.htm

的逻辑应该是:

answer_is_correct = False 

while not answer_is_correct : 

    Keep receiving input until answer is correct 

您可以在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!" 

我希望这对你的作品:

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) 

有在你的代码的两个错误。

  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!") 

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()