【发布时间】:2017-02-18 04:48:59
【问题描述】:
好的,我正在为班级做一个项目,但我遇到了这个逻辑错误。在我在代码中修复它们之后,我已经注意到其余的原始错误......我觉得我知道逻辑错误在哪里。我只是不确定如何解决它。我能得到一些帮助吗?
提前致谢。
import random
def display_title():
print("Guess the number!")
print()
def set_limit():
print("Enter the upper limit for the range of numbers: ")
limit = int(input())
return limit
def count(): ## had to add count being defined as below it was unrecognized by python.
count +=1
def play_game(limit):
global count
number = random.randint(1, limit)
print("I'm thinking of a number from 1 to " + str(limit) + "\n")
while True:
guess = int(input("Your guess: "))
if guess < number:
print("Too low. ")
count ## See def count
elif guess >= number:
print("Too high. ")
count ## See def count
elif guess == number: ## Pretty sure my logic error is here <----
print("You guessed it in " + str(count) + " tries.\n")
return
def main(): ## syntax error, no : was here
display_title()
again = "y"
while again.lower() == "y":
limit = set_limit()
play_game(limit) ## limit wasn't set inside, causing a missing positional argument
again = input("Play again? (y/n): ")
print()
print("Bye!")
if __name__ == "__main__":
main()
【问题讨论】: