0

I'm trying to write a program that will generate a random number between 1 and 100 and then ask the user for a guess. At that point if the guess is correct it will tell them so and vice-versa if it's wrong.

What I have so far is:

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

When I run the program it just gives me <function playGame2 at 0x0000000002D3D2F0>. Why is it doing that?

4

4 回答 4

8

您必须执行该功能,您的输出意味着您做了类似的事情

print(playGame2)

代替

playGame2()
于 2013-09-20T19:56:33.350 回答
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)

试试看。我刚刚运行它工作正常。要在空闲或可以访问它的地方使用 enter playGame2()。

于 2013-09-20T19:59:40.390 回答
0

你需要告诉 python 这有一个 main 函数尝试在你的代码末尾包含这个:

if __name__ == "__main__":
    playGame2()

并将其放在开头:

# -*- coding: UTF-8 -*-

在代码的顶部,用于指定您使用的 Python 文件的编码方式。见http://www.python.org/dev/peps/pep-0263/

希望它会帮助你。

于 2013-09-20T21:00:03.757 回答
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")
于 2016-04-25T09:36:25.053 回答