Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 猜数字游戏,错误。_Python_Python 3.x - Fatal编程技术网

Python 猜数字游戏,错误。

Python 猜数字游戏,错误。,python,python-3.x,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

这是我目前正在尝试制作的游戏。我正在用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!")
#这是一个猜数字游戏!
随机输入
猜测=0
name=输入(“你叫什么名字?”)
number=random.randint(1,20)
print=name+“,我想的是一个介于1和20之间的数字…”
而猜测数字:
打印(“你的猜测太高了!”)
如果guess==数字:
打破
如果guess==数字:
猜测=str(猜测)
打印(“干得好,“+name+”你在“+guesss+”guesss!”中猜到了我的号码)
猜猜看!=编号:
编号=str(编号)
打印(“不,伙计,下次好运!”)

我想你是想用
你的程序有很多错误。在你的问题中一定要包括错误。考虑到您所犯的语法错误,首先通过执行简单命令来掌握python解释器。下面应该会有所帮助。下面是在Python 2中,对于Python 3,用
print('something')替换
raw\u input()
with
input
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()
随机导入
name=原始输入(“你好!你叫什么名字?\n”)
打印“嗯,+name+”,我想的是一个介于1和20之间的数字
否=random.randint(1,20)
guess=int(原始输入(“进行猜测”)
计数=1
猜猜看!=不:
如果猜测<否:
打印“你的猜测太低了。”
如果猜测>否:
打印“你的猜测太高”
计数+=1
guess=int(原始输入(“进行猜测”)
打印“干得好,%s!您在%d次猜测中猜到了我的号码!”%(姓名、计数)
第二种解决方案:

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()
随机导入
def check():
全局计数#使用全局计数的好例子
guess=int(原始输入(“进行猜测”)
如果猜测==否:
打印“干得好,%s!您在%d次猜测中猜到了我的号码!”%(姓名、计数)
elif猜测<否:
打印“你的猜测太低了。”
计数+=1
检查()
其他:
打印“你的猜测太高”
计数+=1
检查()
name=原始输入(“你好!你叫什么名字?\n”)
打印“嗯,+name+”,我想的是一个介于1和20之间的数字
否=random.randint(1,20)
全局计数
计数=1
检查()

您的代码运行良好,稍加修改即可运行

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)
随机导入
猜测=0
name=raw_input(“你叫什么名字?”)#use input()使用的是Python 3
number=random.randint(1,20)
print name+“,我想到的是一个介于1和20之间的数字…”
当猜测<7时:
guess=int(原始输入(“猜一猜”)
猜测=猜测+1
如果猜测<数字:
打印(“你的猜测太低了!”)
如果猜测>数字:
打印(“你的猜测太高了!”)
如果guess==数字:
打破
如果猜测==数字:
打印(“干得好”,姓名,“你猜中我的号码了”,猜中,“猜中了!”)
如果猜中了!=编号:
编号=str(编号)
打印(“不,伙计,下次好运!”,“数字是”,数字)

它究竟以什么方式不运行?包括预期输出、实际输出、错误消息等。虽然我可以看到一些语法错误和打字错误,例如顶部的缩进行,
print=…
如果这里有多余的
,请使用多个
进行猜测。它将检查这些条件中的每一个,而不是在它通过的条件上停止。因此,使用更漂亮的代码,我仍然会得到一个错误。此错误为:syntaxerror:“断开”外部循环您是否尝试了复制和粘贴<代码>中断
肯定在这里的循环中,它可能是复制/粘贴,也可能是编辑错误调整代码。正如错误所述,
中断
必须在循环内,在这种情况下,在
else
内,以在适当的点中断
,而
循环。我不想复制和粘贴。我需要修复出了什么问题,以便直观地看到出了什么问题。不管怎么说,它是固定的,工作得很好。谢谢!