user2468523
user2468523

Reputation: 29

What is wrong with my python code? (Multiplication program)

So I tried to make a multiplication game. So it kinda works, but when I put in the right answer, it types 'Wrong' two times... as well as if I put in the wrong answer.

import sys #imports sys
random1=['1','2','3','4','5','6','7','8','9','10','11','12'] #makes a list with numbers 1-12
random2=['1','2','3','4','5','6','7','8','9','10','11','12'] #makes another list with numbers 1-12

from random import choice #gets a random number
while True: #makes the following code run in a loop
    print "To exit this game type 'exit'" #prints stuff
    theyputinstuffhere = raw_input("What is " + choice(random2) + " times " + choice(random1) + "? ") #asks the user things like 1*5 or some other random stuff.


    if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
        print "Now exiting game!" #prints stuff
        sys.exit() #exits

    elif theyputinstuffhere == int(choice(random2))*int(choice(random1)): #if what i typed is right, print stuff (I think i messed up here!)
        print "Correct!" #print stuff
    else:
       print "Wrong!" #otherwise print stuff 

I don't know what i did wrong HELP!!!!!!!!!!!!! QUICK!!!!!!!!!!!!!!!!!

Upvotes: 1

Views: 580

Answers (4)

user2468523
user2468523

Reputation: 29

THE ANSWER TO THIS QUESTION IS >>>>>> ANSWERED BY CHRISTIAN CAREAGA !!!!!

import sys 
import random

while True: 
    num1 = random.randint(0,12)
    num2 = random.randint(0,12)
    print "To exit this game type 'exit'" 
    theyputinstuffhere = raw_input("What is " + str(num2) + " times " + str(num1) + "? ") 


if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
    print "Now exiting game!" #prints stuff
    sys.exit() #exits

elif int(theyputinstuffhere) == num1*num2: 
    print "Correct!" #print stuff
else:
    print "Wrong!" 

THANK YOU ALL!!!! YOU ALL HELPED!!! (Sorry Christian, i just copied wrong...)

Upvotes: 0

Serial
Serial

Reputation: 8043

this should work perfectly

import sys 
import random

while True: 
    num1 = random.randint(0,12)
    num2 = random.randint(0,12)
    print "To exit this game type 'exit'" 
    theyputinstuffhere = raw_input("What is " + str(num2) + " times " + str(num1) + "? ") 


    if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
        print "Now exiting game!" #prints stuff
        sys.exit() #exits

    elif int(theyputinstuffhere) == num1*num2: 
        print "Correct!" #print stuff
    else:
        print "Wrong!" 

use random.randint instead of choice cause then you dont need a list you were close though!

and you were reseting num1 and num2 when you were using youre if statement now it resets every loop

Upvotes: 1

stackErr
stackErr

Reputation: 4170

instead of

theyputinstuffhere = raw_input("What is " + choice(random2) + " times " + choice(random1) + "? ") #asks the user things like 1*5 or some other random stuff.


if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
    print "Now exiting game!" #prints stuff
    sys.exit() #exits

elif int(theyputinstuffhere) == int(choice(random2))*int(choice(random1)): #if what i typed is right, print stuff (I think i messed up here!)
    print "Correct!" #print stuff
else:
   print "Wrong!" #otherwise print stuff 

do this:

num1 = choice(random1)
num2 = choice(random2)
theyputinstuffhere = raw_input("What is " + num1 + " times " + num2 + "? ") #asks the user things like 1*5 or some other random stuff.


if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
    print "Now exiting game!" #prints stuff
    sys.exit() #exits

elif theyputinstuffhere == int(num1)*int(num2): #if what i typed is right, print stuff (I think i messed up here!)
    print "Correct!" #print stuff
else:
   print "Wrong!" #otherwise print stuff 

Upvotes: 0

Dan Doe
Dan Doe

Reputation: 1166

You are using choice twice, so while what you are asking could be something like this: 5 * 6
Your if statement could check for something like: 11 * 4
You are also comparing int to string, so convert your theyputinstuffhere to int in the conditional as well.

Also, choice() is a bad function for this, if you had used randrange() instead you wouldn't have to create a list at all. Try assigning both of your initial choice() return values to variables, and use those in both your prompt and conditional later.

Upvotes: 1

Related Questions