8 Replies - 2067 Views - Last Post: 16 February 2011 - 02:56 PM Rate Topic: -----

#1 CodyWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 21-February 10

While statement crashes program. stuck in loop

Posted 16 February 2011 - 10:36 AM

Okay, so I am doing random things right now in Python, so I decided to try: the random integer level! How exciting! haha

Anways, my code follows:
import random
rand1 = random.randint(0, 100)
rand2 = random.randint(0, 100)
answer = input(("What is " + str(rand1) + "+" + str(rand2)))
tanswer = rand1 + rand2
while int(answer) != int(tanswer):
    print("Incorrect! Please try again.")
    input(("What is " + str(rand1) + "+" + str(rand2)))
    if int(answer) == int(tanswer):
        print("Correct! Great job! Continuing...")
elif int(answer) == int(tanswer):
    print("Correct! Great job! Continuing...")


2 problems:
1. In PyScripter, elif is incorrect syntax?
2. No matter what, even if I get the correct answer (after getting it wrong once) it continues to say it is incorrect.

What is wrong with my code?

-Cody

Is This A Good Question/Topic? 0
  • +

Replies To: While statement crashes program. stuck in loop

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 10:55 AM

Problem 2.) When you ask the user for another guess, you don't assign that value to answer... you don't do anything with it :)

Problem 1.) Also, that elif doesn't have a corresponding if. The if above it is in the while loop, the elif is outside the while loop. Python doesn't see them as having a relationship to eachother because they are in different scopes.

Here are a few modifications I gave to your code.
1.) I changed input to be a little cleaner. The '\n' tells it to skip a line. (I also removed an extra set of parenthesis you don't need)

2.) if the user is wrong, we go into the while loop, if he's right we congratulate him and end.

import random
rand1 = random.randint(0, 100)
rand2 = random.randint(0, 100)
answer = input("\nWhat is " + str(rand1) + "+" + str(rand2)+": ")
tanswer = rand1 + rand2
if int(answer) != int(tanswer):
    print("Incorrect! Please try again.")
    while int(answer) != int(tanswer):
        answer = input("\nWhat is " + str(rand1) + "+" + str(rand2)+": ")
        if int(answer) == int(tanswer):
            print("Correct! Great job!")
        else:
            print("TRY AGAIN MORTAL!")
else:
    print("You got it on your first try, good for you.")



I always give the computer a little attitude... they'll one day rule us all.

This post has been edited by atraub: 16 February 2011 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 CodyWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 21-February 10

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:11 PM

Thanks so much! That works perfectly :)

Now a second question, I want the program to continue until they X it out.

How do I make it go back to the beginning of the script, choose two new random integers, and then have them guess again? At this point, I'm lost :) And googling did not find anything on restarting a script.
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:12 PM

1 loop to rule them all :)

import random
while True:
    rand1 = random.randint(0, 100)
    rand2 = random.randint(0, 100)
    answer = input("\nWhat is " + str(rand1) + "+" + str(rand2)+": ")
    tanswer = rand1 + rand2
    if int(answer) != int(tanswer):
        print("Incorrect! Please try again.")
        while int(answer) != int(tanswer):
            answer = input("\nWhat is " + str(rand1) + "+" + str(rand2)+": ")
            if int(answer) == int(tanswer):
                print("Correct! Great job!")
            else:
                print("TRY AGAIN MORTAL!")
    else:
        print("You got it on your first try, good for you.")



Aside from adjusting the indentation, adding the main loop is the ONLY change I made. Incidentally, this program has no ability to exit the loop. This is known as an infinite loop and you should try to avoid em... but it sounds like an infinite loop is exactly what you were looking for :walkman:

This post has been edited by atraub: 16 February 2011 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 CodyWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 21-February 10

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:19 PM

Yay, I'll try this one out :) Thanks. Lets see if it works..

WOOT WOOT! It worked! Thanks so much atrab, now lets see if I can get them the ability to choose between multiple options (Like real numbers, integers, the range, subtraction, etc...)

I will see if there is a better way to do it once I do get it :)
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:24 PM

you know, on my blog I talk about a few topics that may be helpful to you:

Was This Post Helpful? 0
  • +
  • -

#7 CodyWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 21-February 10

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:41 PM

The division section isn't working at all! D: It tells me that it's incorrect, even when rounding to the hundreths (1.000 etc). This is my code:
##################################################################
## Author: Cody Dostal
## Description: A flash card program for basic math.
## Developed on: Windows 7
## Language: Python 3.2
##################################################################



import random # Getting required files to run this program.

print("Welcome to the PyFlashCard program! This will test your math abilites\n")
print("as well as test all aspects. We have some questions for you to learn\n")
print("what kind of tests you are looking for.")

TOM = input("What kind of math are you looking for? Type 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.") # TOM = Type of Math
if TOM == str(1):
    print ("You chose addition as your type of math.")
    while True:
        rand = random.randint # Simplify the random input
        rand1 = rand(0, 100) # Choose a random integer from 0 to 100
        rand2 = rand(0, 100) # Choose a random integer from 0 to 100
        answer = input("\nWhat is " + str(rand1) + "+" + str(rand2)+": ") # Asks them to type an answer to the question.
        tanswer = rand1 + rand2 # The actual correct answer, so we can compare the answer later.
        if int(answer) != int(tanswer): # Does their answer equal the real answer? If not...
            print("Incorrect! Please try again.") # Tell them it's wrong
            while int(answer) != int(tanswer): # Go into a loop, if it's wrong again, we make them reanswer it.
                answer = input("\nWhat is " + str(rand1) + "+" + str(rand2)+": ") # Re-state the question.
                if int(answer) == int(tanswer): # Once they get it right, we leave the loop
                    print("Correct! Great job!") # Congratulate them
        else: # If they STILL don't get it right
            print("TRY AGAIN MORTAL!") # We condemn them!
    else:
        print("You got it on your first try, good for you.") # If they didn't mess up once, we don't enter the loop.
elif TOM == str(2):
    print ("You chose subtraction as your type of math.")
    while True:
        rand = random.randint # Simplify the random input
        rand1 = rand(0, 100) # Choose a random integer from 0 to 100
        rand2 = rand(0, 100) # Choose a random integer from 0 to 100
        answer = input("\nWhat is " + str(rand1) + "-" + str(rand2)+": ") # Asks them to type an answer to the question.
        tanswer = rand1 - rand2 # The actual correct answer, so we can compare the answer later.
        if int(answer) != int(tanswer): # Does their answer equal the real answer? If not...
            print("Incorrect! Please try again.") # Tell them it's wrong
            while int(answer) != int(tanswer): # Go into a loop, if it's wrong again, we make them reanswer it.
                answer = input("\nWhat is " + str(rand1) + "-" + str(rand2)+": ") # Re-state the question.
                if int(answer) == int(tanswer): # Once they get it right, we leave the loop
                    print("Correct! Great job!") # Congratulate them
        else: # If they STILL don't get it right
            print("TRY AGAIN MORTAL!") # We condemn them!

elif TOM == str(3):
    print ("You chose multiplication as your type of math.")
    while True:
        rand = random.randint # Simplify the random input
        rand1 = rand(0, 100) # Choose a random integer from 0 to 100
        rand2 = rand(0, 100) # Choose a random integer from 0 to 100
        answer = input("\nWhat is " + str(rand1) + "*" + str(rand2)+": ") # Asks them to type an answer to the question.
        tanswer = rand1 * rand2 # The actual correct answer, so we can compare the answer later.
        if int(answer) != int(tanswer): # Does their answer equal the real answer? If not...
            print("Incorrect! Please try again.") # Tell them it's wrong
            while int(answer) != int(tanswer): # Go into a loop, if it's wrong again, we make them reanswer it.
                answer = input("\nWhat is " + str(rand1) + "*" + str(rand2)+": ") # Re-state the question.
                if int(answer) == int(tanswer): # Once they get it right, we leave the loop
                    print("Correct! Great job!") # Congratulate them
        else: # If they STILL don't get it right
            print("TRY AGAIN MORTAL!") # We condemn them!

elif TOM == str(4):
    print ("You chose division as your type of math.")
    while True:
        rand = random.randint # Simplify the random input
        rand1 = rand(0, 100) # Choose a random integer from 0 to 100
        rand2 = rand(0, 100) # Choose a random integer from 0 to 100
        answer = input("\nWhat is " + str(rand1) + "/" + str(rand2)+" (Round to the thousandths (0.000): ") # Asks them to type an answer to the question.
        tanswer = rand1 / rand2 # The actual correct answer, so we can compare the answer later.
        if float(answer) != float(tanswer): # Does their answer equal the real answer? If not...
            print("Incorrect! Please try again.") # Tell them it's wrong
            while float(answer) != float(tanswer): # Go into a loop, if it's wrong again, we make them reanswer it.
                answer = input("\nWhat is " + str(rand1) + "/" + str(rand2)+": ") # Re-state the question.
                if float(answer) == float(tanswer): # Once they get it right, we leave the loop
                    print("Correct! Great job!") # Congratulate them
        else: # If they STILL don't get it right
            print("TRY AGAIN MORTAL!") # We condemn them!

else:
    print("Please choose a valid option next time.")



This post has been edited by CodyWD: 16 February 2011 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:42 PM

for the purposes of this program, try using integer division. To use integer division, replace this symbol "/" with "//". Integer division cuts off any remainders. For example, 5//2 = 2 (the .5 is removed). The reason is that there are some values that can't be expressed in binary. For example, in decimal we can say that 1/2 is .5. However, we can't display 1/3, instead we have .333333333333333333333333333333333333333333333 that goes on forever. You run into a similar problem with computers. It's usually a bad idea to compare quotients unless you absolutely have to (and there are good ways to do that).

To prove it to yourself, open up IDLE and type this into your shell:
.1 + .2 == .3
The output might surprise you!


Also, there is such a thing as too many comments :) Lines of code that are easily understood really don't need a comment, otherwise it just clutters things.

This post has been edited by atraub: 16 February 2011 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: While statement crashes program. stuck in loop

Posted 16 February 2011 - 02:56 PM

If you must compare quotients, try this:

def almostEqual(a,B)/>:
    if abs(a-B)/><0.0000000001:
        return True
    else:
        return False


Here's another good source to check out.
MIT lecture, ints and floats

This post has been edited by atraub: 16 February 2011 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1