English 中文(简体)
外交部
原标题:Number Guesser Program Malfunction [duplicate]
  • 时间:2011-07-22 19:46:13
  •  标签:
  • python
This question already has answers here:
Closed 12 years ago.

Possible Duplicate:
Learning Python: If condition executing all the time

This Python code is for guessing a number from 1 to 100. There is nothing wrong with the guesser() function, which I know because my program also has a "manual mode" that works perfectly. For some reason, it always assigns the guesss value to low and keeps doing so until it equals 100 and the program "Ragequits". It never does anything to high , which stays at its default throughout the execution. No matter what, it never executes the "winning" block. NOTE: The variables high and low start as 101 and 0 respectively, and guesser() chooses a number between them. goOn is used earlier in the code to determine if the person wants to play again.

            num = raw_input( Enter the number which you want the computer to guess.  )
            unguessed = True
            while unguessed:
                if high == low + 1 or high <= low:
                    print  Waitafligginflagginminnit! You CHEATED! *Ragequits* 
                    goOn = False
                    unguessed = False
                    print   
                    raw_input( Press Enter to continue. )
                guesses = guesses + 1
                guesss = guesser()
                print  I guessed , guesss
                if guesss == num:
                    print  Yay! I won in , guesses,  guesses! 
                    again = raw_input( Just press enter if you want to play again. Otherwise...you know the drill.  )
                    unguessed = False
                    print  

 
                    if again:
                        goOn = False
                else:
                    print  Awww... 
                    if guesss > num:
                        high = guesss
                    elif guesss < num:
                        low = guesss
最佳回答

您应使用<代码>int(raw_input(),而不是raw_input,以便保留这些gues作为ger。 <代码>raw_input(> autrelease a string, 因而无法进行比较。

见这些先前的答复:,以及这一准确的

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签