运行脚本时出现奇怪的行为

时间:2016-10-15 14:40:10

标签: python pycharm

我开始猜测游戏的数字类型。当我执行程序时,它要么完美流动,要么不起作用......

UPDATE `tbl_shift_details`  
SET EndTime  = '10:00:00' EndDate = '2016-10-15',
    EndBillNo = '50',`TempColl` = (Val(TempColl) + '500') 
WHERE `OperatorId` =  '3' AND machineId = '026'
ORDER BY id DESC LIMIT 1;

我完全不知道是否因为代码而发生,或者pycharm是怪罪!

2 个答案:

答案 0 :(得分:0)

你真的需要一个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

现在我建议你添加一个玩家在正确的猜测之前的猜测次数,并在最后打印出来!

编辑:您会注意到我的import声明与@Denis Ismailaj的声明不同。我们都同意只需要一个import,但对哪一个持不同意见。在我的版本import random中,这意味着你必须说random.randint而在另一个版本中你只说randint

在一个小程序中,它们之间没有多大选择,但程序永远不会变小。一个更大的程序,可以轻松导入6,7,8或更多模块,有时很难追踪函数来自哪个模块 - 这被称为名称空间污染。与randint这样的知名函数不会混淆,如果你明确地给出了函数的名称,那么它就可以很容易地被追踪回来。这只是个人偏好和风格的问题。

增加了猜测次数:

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

答案 1 :(得分:-1)

以下是已作为答案提供的2个主题的变体 在这个选项中,猜测部分被定义为一个函数,它允许我们提供重复玩游戏的选项,直到用户感到无聊僵硬! 提供的另一个选择是不要求用户输入&#34;是&#34;或&#34;不&#34;但只是以&#34; y&#34;开头的东西或&#34; n&#34;。

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