12 Replies - 1165 Views - Last Post: 23 June 2012 - 09:54 PM Rate Topic: -----

#1 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 20 June 2012 - 03:43 AM

Okay so there are two errors here. One; instead of saying wrong when I get the math equation wrong (this is a simple math equation testing script) it will display the "Numbers Only!" elif statement (it's pulling those lists from up top). Two; down the bottom of the code is the if "easy" blah blah; that's linked to the moade = raw_input("> ") up the top. When I type something other than those 3 things, it just takes me to my questions anyway. Would really appreciate any help anyone could give me!

from sys import exit
from random import randint

lettersL = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
lettersU = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
symbols = [',','.','/',';','[',']','=','-','<','>','"','?',':','{','}','|','+','_',')','(','*','&','^','%','$','#','@','!','`','~']
 
print """
Welcome to simple maths training!
You will be tested on simple maths equations.
Note; Answers should only contain numbers.
"""

print "Choose your difficulty! (Easy/Medium/Hard)"
mode = raw_input("> ")

class Game(object):
 
	def question(self): #Loop
 
		while "easy" or "Easy" or "EASY" in mode:
			f1 = randint(1,10) #SIMPLIFY THIS
			f2 = randint(1,10) #SIMPLIFY THIS
			print "What is %r + %r?" % (f1, f2)
			answerIn = "%s" % (f1 + f2) #SIMPLIFY THIS
			answer = raw_input("> ") #SIMPLIFY THIS
 
			if lettersL[0-25] or lettersU[0-25] or symbols[0-29] in answer:
				print "Numbers Only!"
			elif answer != (f1+f2):
				print "Wrong!"
 
			if answer == answerIn:
				print "Correct!"
			elif lettersL[0-25] or lettersU[0-25] or symbols[0-29] in answer:
				print "Numbers Only!"
			elif answer != (f1+f2):
				print "Wrong!"
				
				
if "easy" or "Easy" or "EASY" in mode:
	new_game = Game()
	new_game.question()
else:
	"Sorry, I didn't understand that. try again!"


Very sloppy, I know. I'm a beginner.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Python (Object Oriented) - Bugs (Math Equations)

#2 witeboy724  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 21-June 12

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 21 June 2012 - 11:39 PM

I pressed 'back' by accident on this after typing everything up. My fault, not anyone else's, just ugh. Anyway..

Line 28 is a little incorrect.

if lettersL[0-25] or lettersU[0-25] or symbols[0-29] in answer:

It's asking lettersL, lettersU, and symbols to see if it has a value in any of them without running through all their values to check against 'answer'. If that sounds confusing that's because I suck at explaining things. I'll show you an example of something that works and see if that helps. It'll basically check each value in the 3 things against answers. I am also just a beginner, and this most likely isn't the quickest or most correct way, but at least it should work for what you're trying to do.


for x in len(lettersL):  #check every item in lettersL, 25 in this case
    if lettersL[x] in answers: foundit = true

for x in len(lettersU):  
    if lettersU[x] in answers: foundit = true

for x in len(symbols):  
    if symbols[x] in answers: foundit = true

if foundit == true:
    #do something spectacular



Was This Post Helpful? 1
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 22 June 2012 - 02:44 PM

This "easy" or "Easy" or "EASY" in mode does not work. It will always evaluate to true no matter what is in mode. What you should do is something like if mode.lower() == "easy".
Was This Post Helpful? 2
  • +
  • -

#4 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 22 June 2012 - 10:38 PM

View Postwiteboy724, on 21 June 2012 - 11:39 PM, said:

I pressed 'back' by accident on this after typing everything up. My fault, not anyone else's, just ugh. Anyway..

Line 28 is a little incorrect.

if lettersL[0-25] or lettersU[0-25] or symbols[0-29] in answer:

It's asking lettersL, lettersU, and symbols to see if it has a value in any of them without running through all their values to check against 'answer'. If that sounds confusing that's because I suck at explaining things. I'll show you an example of something that works and see if that helps. It'll basically check each value in the 3 things against answers. I am also just a beginner, and this most likely isn't the quickest or most correct way, but at least it should work for what you're trying to do.


for x in len(lettersL):  #check every item in lettersL, 25 in this case
    if lettersL[x] in answers: foundit = true

for x in len(lettersU):  
    if lettersU[x] in answers: foundit = true

for x in len(symbols):  
    if symbols[x] in answers: foundit = true

if foundit == true:
    #do something spectacular




Would I replace [x] with [0,25] or [0-25]. Still learning as I go along.

Thank you both for your replies!
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 07:58 AM

I should also mention that this syntax lettersL[0-25] or lettersU[0-25] or symbols[0-29] in answer also doesn't work for several reasons. If you're trying to say, from 0 to 25, you're not, you're actually saying, 0 minus 25, which is negative 24 25, which equates to 'b' or 'B' in your setters lists. Also, like the other one, this will always equate to true. The or statement let's you compare multiple conditions, for example, it would make sense to say if x > 30 or y > 30: because we are comparing two conditions. If we said if if x or y > 30: then what we're really saying is if bool(x) or y > 30:. If x is any non-zero number, then this statement will always equate to true.

EDIT:
To Answer your question, who says you need to replace x?

EDIT:
I swear I can do basic arithmetic!!

This post has been edited by atraub: 23 June 2012 - 07:59 PM

Was This Post Helpful? 2
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 07:56 PM

View Postatraub, on 23 June 2012 - 04:58 PM, said:

0 minus 25, which is negative 24


Negative 25.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 07:58 PM

wow, that was an embarassing typo :blush: At least I had the right position in the list.

This is an off-day for me! I blame the AC being broken at my house (the temperature has been in the 90's all week!). It's like Ninedy Ayte degrees in my howse!

This post has been edited by atraub: 23 June 2012 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 08:49 PM

With the new method pulling the lists, I'm getting this error.

Taceback (most recent call last):
        File "ex45.py", line 50, in <module>
          new_game.question()



With the new method pulling the lists, I'm getting this error.

Class
class Game(object):
 
	def question(self): #Loop
 
		while "easy" or "Easy" or "EASY" in mode:
			f1 = randint(1,10) #SIMPLIFY THIS
			f2 = randint(1,10) #SIMPLIFY THIS
			print "What is %r + %r?" % (f1, f2)
			answerIn = '%s' % (f1 + f2) #SIMPLIFY THIS
			answer = raw_input("> ") #SIMPLIFY THIS
			
			for x in len(lettersL): 
				if lettersL[x] in answers: foundit = true
			for x in len(lettersU):
				if lettersU[x] in answers: foundit = true
			for x in len(symbols): 
				if symbols[x] in answers: foundit = true
			
			if foundit == true:
				print "Numbers Only! Question Passed!"
			elif answer == answerIn:
				print "Correct!"
			else:
				print "Wrong!"


Error
Taceback (most recent call last):
        File "ex45.py", line 50, in <module>
          new_game.question()
        File "ex45.py", line 50, in question
          for x in len(lettersL):
TypeError: 'int' object is not iterable


Sorry for double posting! Stupid computer!
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 09:02 PM

The code witeboy gave you is wrong. You can't do for x in len(foo):. for x in something expects something to be iterable, but len returns an int and ints aren't iterable.

You should just do for letter in lettersL: and then use letter instead of lettersL[x] in the loop body (and the same for the other loops).
Was This Post Helpful? 1
  • +
  • -

#10 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 09:22 PM

I can see it's starting to get better, thanks a lot for that answer but I'm getting an UnBoundLocal error, where it says:
if foundit == true:


"local variable 'foundit' referenced before assignment"

I've run into this issue plenty of times before but never got around it.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 09:44 PM

The problem is that the variable foundit only exists in the loops. You need to set it once before the loops, so that it exists in the outside scope (also so that it has a value even if the loops never set it to true). In other words: set foundit to False before the for loops.
Was This Post Helpful? 2
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 09:47 PM

Just initialize foundit to False at the beginning of the while loop.

EDIT:
sepp2k beat me to it.

This post has been edited by atraub: 23 June 2012 - 09:49 PM

Was This Post Helpful? 1
  • +
  • -

#13 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Re: Simple Python (Object Oriented) - Bugs (Math Equations)

Posted 23 June 2012 - 09:54 PM

Okay I finally have it, I kept using "false" instead of "False", noticed by the syntax highlighting (idiotic). Thank you so much for your help guys! I'll be sure to remember this in future!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1