我正在尝试编写一个程序,该程序将生成1到100之间的随机数,然后询问用户猜测。此时,如果猜测是正确的,它会告诉他们,反之亦然,如果这是错误的。
到目前为止我所拥有的是:
import random
def playGame2():
number = random.randint(1,100)
guess = input("I'm thinking of a number between 1 and 100. Guess what it is: ")
if str(number) == guess:
print("That is correct!")
else:
print("Nope, I was thinking of" +str(number))
当我运行该程序时,它只给我<function playGame2 at 0x0000000002D3D2F0>
。
为什么这样做?
答案 0 :(得分:8)
您必须执行该功能,您的输出意味着您执行了类似
的操作print(playGame2)
而不是
playGame2()
答案 1 :(得分:0)
def playGame2():
number = random.randrange(1,100)
guess = input("I'm thinking of a number between 1 and 100. Guess what it is: ")
if str(number) == guess:
print("That is correct!")
else:
print("Nope, I was thinking of %s" % number)
试试吧。我刚刚运行它工作正常。要在空闲时使用输入playGame2()或可以访问它的地方。
答案 2 :(得分:0)
你需要告诉python它有一个主要功能 尝试在代码末尾包含此内容:
if __name__ == "__main__":
playGame2()
并将其放在开头:
# -*- coding: UTF-8 -*-
位于代码顶部,用于指定您使用的Python文件的编码方式。见http://www.python.org/dev/peps/pep-0263/
希望它会对你有所帮助。
答案 3 :(得分:0)
import random
guess=99
count=0
no = random.randint(1,100)
print("Welcome to the guessing game!")
while guess != no :
guess = int(input("Guess a number: "))
if guess < no :
print("Higher")
count+=1
if guess > no :
print("Lower")
count+=1
if (guess==no):
print("You win, the number was indeed:",no)
print("It took you a total of:",count,"tries")