Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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_Pycharm - Fatal编程技术网

Python 运行脚本时的奇怪行为

Python 运行脚本时的奇怪行为,python,pycharm,Python,Pycharm,我开始编写一个猜测数字类型的游戏。当我执行这个程序时,它要么运行正常,要么不工作 import random from random import randint print("Welcome to guess the number!\nDo you want to play the game?") question = input("") if question == "Yes".lower(): print("Sweet! Let`s begin!\nGuess the number

我开始编写一个猜测数字类型的游戏。当我执行这个程序时,它要么运行正常,要么不工作

import random
from random import randint

print("Welcome to guess the number!\nDo you want to play the game?")

question = input("")

if question == "Yes".lower():
print("Sweet! Let`s begin!\nGuess the number between 1 and 10!")

    number = random.randint(1, 10)
    guess = int(input("Take a guess!\n"))

    if guess > number:
        print("Your guess is too high")
        guess = int(input("Take a guess!\n"))

    if guess < number:
        print("Your guess is too low")
        guess = int(input("Take a guess!\n"))

    if guess == number:
        print("Your guess was correct!")

elif question == "No".lower():
    print("Too bad! Bye!")
    quit()
随机导入
从随机导入randint
打印(“欢迎猜号码!\n您想玩这个游戏吗?”)
问题=输入(“”)
如果问题==“是”。降低()
打印(“好极了!让我们开始吧!\n请输入介于1和10之间的数字!”)
number=random.randint(1,10)
guess=int(输入(“猜测!\n”))
如果猜测>数字:
打印(“您的猜测太高”)
guess=int(输入(“猜测!\n”))
如果猜测<数字:
打印(“您的猜测太低”)
guess=int(输入(“猜测!\n”))
如果guess==数字:
打印(“您的猜测是正确的!”)
elif question==“否”。lower():
打印(“太糟糕了!再见!”)
退出

我完全不知道这是因为代码,还是pycharm是罪魁祸首

您确实需要一个while循环。如果第一个猜测太高,那么你会有另一个机会,但它只是测试它是否太低或相等。如果你的猜测太低,那么你的第二次机会必须是正确的

因此,您不需要继续测试,您可以简化,但您应该在设计阶段真正做到这一点。以下是我的版本:

import random
#  from random import randint   << no need for this line

print("Welcome to guess the number!")

question = input("Do you want to play the game? ")

if question.lower() == "yes":
    print("Sweet! Let`s begin!\nGuess the number between 1 and 10!")

    number = random.randint(1, 10)
    guess = None                     # This initialises the variable

    while guess != number:           # This allows continual guesses

        guess = int(input("Take a guess!: ")) # Only need one of these

        if guess > number:
            print("Your guess is too high")
        elif guess < number:     
            print("Your guess is too low")
        else:   # if it isn't < or > then it must be equal!
            print("Your guess was correct!")

else:
    # why do another test?  No need.
    print("Too bad! Bye!")

    # No need for quit - program will exit anyway
    # but you should not use quit() in a program
    # use sys.exit() instead

以下是已作为答案提供的两个主题的变体。
在这个选项中,猜测部分被定义为一个函数,它允许我们提供重复玩游戏的选项,直到用户感到厌烦为止! 提供的另一个选项是不要求用户输入“是”或“否”,而只要求输入以“y”或“n”开头的内容

来自随机导入randint
def game():
打印(“猜1到10之间的数字!”)
number=randint(1,10)
猜测=无
猜猜看!=编号:
guess=int(输入(“猜测!\n”))
如果猜测>数字:
打印(“您的猜测太高”)
elif guess<数字:
打印(“您的猜测太低”)
其他:
打印(“您的猜测是正确的!”)
返回输入(“另一个游戏y/n”)#返回一个值,该值确定游戏是否会重复
打印(“欢迎猜号码!\n您想玩这个游戏吗?”)
问题=输入(“”)
repeat=“y”#定义repeat以便进行第一次迭代
如果question.lower().startswith('y')==True:
打印(“好极了!让我们开始吧!”)
while repeat.lower().startswith('y')==True:#while repeat==“y”继续播放
repeat=game()#玩游戏并将repeat变量设置为返回的值
打印(“再见!”)

大家好,欢迎来到伟大的程序世界。在我们的世界中,“不起作用”表示为详细解释和堆栈跟踪(如果引发异常)。需要缩进
print('Sweet'…
),如果
question==“Yes”。lower()
改为
question.lower()==“Yes”
。除此之外,您的应用程序还可以正常工作,尽管这取决于您想要实现的目标。我在使用os.exit()时就知道了这一点:此检查检测应解析但不应解析的名称。由于动态分派和duck类型,这在有限但有用的情况下是可能的。顶级和类级项目比实例项目更受支持。我尝试使用sys.exit(),但效果相同。.我的错,应该是
sys.exit()
可能来自PyCharm?
quit()
只应在交互式python中使用-请参阅(
exit()
quit()
执行相同的操作)另外,对于猜测的数量,我试着写这个:编辑:对于猜测的数量,我试着写这个:nrgesses=0,而nrgesses<6:nrgesses+=1也试着将它缩进所有地方,但是返回的nrgesses总是6…Idk如何解决这个问题,有什么提示吗?试着
而猜测!=数量和nrgesses<6:
。不确定“处处缩进”是什么意思-你必须有选择性!
import random

print("Welcome to guess the number!")

question = input("Do you want to play the game? ")

if question.lower() == "yes":
    print("Sweet! Let`s begin!\nGuess the number between 1 and 10!")

    number = random.randint(1, 10)
    guess = None
    nrGuesses = 0                    # Added

    # Allow for continual guesses
    while guess != number and nrGuesses < 6:    # Changed

        guess = int(input("Take a guess!: ")) # Only need one of these

        nrGuesses += 1               # Added

        if guess > number:
            print("Your guess is too high")
        elif guess < number:
            print("Your guess is too low")
        else:   # if it isn't < or > then it must be equal!
            print("Your guess was correct!")

else:
    print("Too bad! Bye!")

print("Number of guesses:", nrGuesses) # Added
from random import randint

def game():
    print("Guess the number between 1 and 10!")
    number = randint(1, 10)
    guess = None
    while guess != number:
        guess = int(input("Take a guess!\n"))
        if guess > number:
            print("Your guess is too high")
        elif guess < number:
            print("Your guess is too low")
        else:
            print("Your guess was correct!")

    return input("Another game y/n ") #return a value which determines if the game will be repeated

print("Welcome to guess the number!\nDo you want to play the game?")
question = input("")
repeat = "y" # define repeat so that the first iteration happens

if question.lower().startswith('y') == True:
    print("Sweet! Let`s begin!")
    while repeat.lower().startswith('y') == True: # while repeat == "y" keep playing
        repeat = game() # play the game and set the repeat variable to what is returned
print("Bye Bye!")