猜数字游戏,错误。

时间:2016-04-27 04:02:07

标签: python python-3.x

这是我目前正在努力制作的游戏。我在python 3.4中编写这个游戏。它没有运行。

    # this is a guess the number game!   

import random    
guesses = 0     
name = input("what is your name?")    
number = random.randint(1, 20)    
print = name + ", I am thinking of a number between 1 and 20..."    
while guesses << 7:
    guess = int(raw_input("Take a guess."))    
    guesses = guesses + 1    
    if guess < number:
        print ("your guess is too low!")    
    if guess > number:
        print ("your guess is too high!")    
    if guess == number:
        break
if guess == number:
    guesses = str(guesses)
    print ("Good job," + name + "you guessed my number in" +guesses +"guesses!")
if guess != number:
    number = str(number)
    print ("Nah dude, better luck next time!")

3 个答案:

答案 0 :(得分:3)

我认为您打算使用<代替<<<<>>分别是左移和右移位运算符。

你的最后两个如果条件也在你的循环之外,并没有多大意义。您已经检查if guess == number一次,如果符合该条件则会中断。 if guess != number您已经分别使用<>检查了这一点。

print = ...print语法为print(some_stuff, ...)。缩进也在顶部关闭,但假设刚刚发布您的第一个问题。

另外,raw_input用于python2,它只是python3中的input。您可以使用%格式化程序或使用.format清除打印报表。

修正代码:(自那以后的Python 3版本&#39;标记在问题中......)

import random

name = input("what is your name?")
number = random.randint(1, 20)
#print("%s I am thinking of a number between 1 and 20..." % name)
print(name + " I am thinking of a number between 1 and 20...")

guesses = 0
while guesses < 7:
    guess = int(input("Take a guess."))
    guesses += 1

    if guess < number:
        print ("your guess is too low!")

    elif guess > number:
        print ("your guess is too high!")

    else:
        #print("Good job %s you guessed my number in %d guesses" % (name, guesses))
        print ("Good job, " + name + " you guessed my number in " + str(guesses) + " guesses!")
        break

答案 1 :(得分:0)

您的计划中有很多错误。始终包含您在问题中遇到的错误。鉴于语法错误,您首先要通过执行简单命令来解决python解释器问题。下面应该有帮助。下面是Python 2,用于Python 3替换,raw_input()inputprint 'something'print ('something')

第一个解决方案:

import random

name = raw_input("Hello! What is your name?\n")
print "Well, " + name + ", I am thinking of a number between 1 and 20"
no = random.randint(1,20)
guess = int(raw_input("Take a guess\n"))
count =1

while guess != no:    
    if guess < no:
        print "Your guess is too low."                 
    if guess > no:
        print "Your guess is too high"
    count +=1
    guess = int(raw_input("Take a guess\n"))

print "Good job, %s! You guessed my number in %d guesses!" % (name ,count)

第二个解决方案:

import random

def check():
    global count  # good example of use of global
    guess = int(raw_input("Take a guess\n"))
    if guess == no:
        print "Good job, %s! You guessed my number in %d guesses!" %(name,count)
    elif guess < no:
        print "Your guess is too low."
        count +=1        
        check()          
    else:
        print "Your guess is too high"
        count +=1
        check()

name = raw_input("Hello! What is your name?\n")
print "Well, " + name + ", I am thinking of a number between 1 and 20"
no = random.randint(1,20)
global count
count  =1
check()

答案 2 :(得分:0)

你的代码很好,很少有变化可以让它运行!

import random    
guesses = 0     
name = raw_input("what is your name?") # use input() is using Python 3
number = random.randint(1, 20)    
print name + ", I am thinking of a number between 1 and 20..."    
while guesses < 7:
    guess = int(raw_input("Take a guess."))    
    guesses = guesses + 1    
    if guess < number:
        print ("your guess is too low!")    
    if guess > number:
        print ("your guess is too high!")    
    if guess == number:
        break
if guesses == number:
    print ("Good job,", name, "you guessed my number in", guesses, "guesses!")
if guesses != number:
    number = str(number)
    print ("Nah dude, better luck next time!", "The number is", number)