简体   繁体   English

猜数 Python 2.7

[英]Guess Number Python 2.7

I am having trouble with this program.我在使用这个程序时遇到了问题。 It is suposed to generate a random whole number between 1 and 1000 but it will generate something like 627.68969869867986985747640967487598563876586805465097080967576587609787587658976098它应该生成一个 1 到 1000 之间的随机整数,但它会生成类似 627.689698698679869857476409674875985638765868054675765876986765876976878

 from random import *
 def main():

    numGuess = input ("guess your number")
    ranNum = randint(8, 100000)
    while ranNum <> numGuess:
        print numGuess
        numGuess = input ("guess your number")
        print numGuess
        ranNum = randint(0,1000)
        if numGuess > ranNum :
            print("ugg, your number is too big")
        else:
            print("whoops, your number is too small")
        if numGuess == ranNum :
            print("YAHOO, YOU GOT IT RIGHT!!!!!!!!")
   main()

could someone tell me why this is not working (it was ran in a python 2.7 editor) also, The first time you enter your number it does not tell you if you should guess higher or lower.有人可以告诉我为什么这不起作用(它是在 python 2.7 编辑器中运行的),当您第一次输入您的数字时,它不会告诉您是否应该猜测更高或更低。

A few things.一些东西。 You generally shouldn't use * in your Python imports ( from module import * ).您通常不应在 Python 导入中使用 * ( from module import * )。 If you're only using one function from the module, it is much safer and your code will be more easily understood if you use from random import randint .如果您只使用模块中的一个函数,那么使用from random import randint会更安全,并且您的代码将更容易理解。

You are reassigning your ranNum after the first guess.您在第一次猜测后重新分配您的 runNum。 Your first guess could change to a correct answer once ranNum has been reassigned to a new value.一旦将 ranNum 重新分配给新值,您的第一个猜测可能会更改为正确答案。

Also, your description says you are looking to generate a random number between 1 and 1000, but your first assignment of ranNum is ranNum == randint(8, 10000) and that will generate a random int between 8 and 10000.此外,您的描述说您希望生成一个 1 到 1000 之间的随机数,但是您对 ranNum 的第一个分配是ranNum == randint(8, 10000) ,这将生成一个 8 到 10000 之间的随机整数。

My suggestion is to remove the name assignments that are outside of the loop and change your while condition to while True: then in your if numGuess == ranNum: logic, add a break statement to break the loop inside main().我的建议是删除循环外的名称分配并将您的 while 条件更改为while True:然后在您的if numGuess == ranNum:逻辑中,添加一个break语句来中断 main() 内的循环。

As for your question, I don't believe you're getting a long result such as the one you provided by using randint().至于您的问题,我不相信您会得到像使用 randint() 提供的结果那样长的结果。 Is there a part of your code you have left out?您是否遗漏了一部分代码?

A few things are either redundant or done wrong, the following is a modification of your solution with a few changes:一些事情要么是多余的,要么是做错了,以下是对您的解决方案的修改,并进行了一些更改:

from random import *
def main():

    numGuess = -1
    ranNum = randint(1, 1000)
    while ranNum <> numGuess:        
        numGuess = input ("guess your number")
        print numGuess        
        if numGuess > ranNum :
            print("ugg, your number is too big")
        elif numGuess < ranNum:
            print("whoops, your number is too small")
        else:
            print("YAHOO, YOU GOT IT RIGHT!!!!!!!!")
main()
  • you ask for the user's input twice in the beginning because of the redundant call before the while loop由于while循环之前的冗余调用,您在开始时要求用户输入两次
  • The call to ranNum = randint(8, 100000) - not neededranNum = randint(8, 100000)的调用 - 不需要
  • You want to do ranNum = randint(0,1000) only once, before you start looping在开始循环之前,您只想执行ranNum = randint(0,1000)一次
  • The if/else/if mechanism - can be improved (see above) if/else/if 机制 - 可以改进(见上文)

Try this one, just corrected in your code..试试这个,只是在你的代码中更正了..

from random import *
def main():

    numGuess = 1
    ranNum = 10
    while ranNum <> numGuess:
        ranNum = randint(1,1000)
        numGuess = input ("guess your number")
        #print ranNum, "random"
        if numGuess > ranNum :
            print("ugg, your number is too big")
        elif numGuess < ranNum:
            print("whoops, your number is too small")
        else:
            print("YAHOO, YOU GOT IT RIGHT!!!!!!!!")
main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM