473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why will this not do anything?

Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2.  
  3. class firstRoom():
  4.     def __init__(self):
  5.         print "Welcome to the start of the game. Good luck finishing it."
  6.         print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
  7.         return 'break_out'
  8.  
  9.     def break_out(self):
  10.  
  11.         # Until everythin is working only one number
  12.         combination = "%d" % (randint(1,3))
  13.         guesses = 0
  14.         guess = raw_input("[KEYPAD]>")
  15.  
  16.  
  17.         while guess != combination and guesses < 10:
  18.             print "INCORRECT, TRY AGAIN"
  19.             guesses += 1
  20.             return 'break_out'
  21.  
  22.         if guess == combination:
  23.             print "welldone"

I do not get any errors, it just doesn't do anything.

This is my first time working with classes by the way.
Jan 27 '12 #1

✓ answered by Smygis

I suspect that all the places you have
Expand|Select|Wrap|Line Numbers
  1. return 'break_out'
That what you really want is self.break_out()
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2.  
  3. class firstRoom():
  4.     def __init__(self):
  5.         print "Welcome to the start of the game. Good luck finishing it."
  6.         print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
  7.  
  8.         self.break_out()
  9.  
  10.     def break_out(self):
  11.  
  12.         # Until everythin is working only one number
  13.         combination = "%d" % (randint(1,3))
  14.         guesses = 0
  15.         guess = raw_input("[KEYPAD]>")
  16.  
  17.  
  18.         while guess != combination and guesses < 10:
  19.             print "INCORRECT, TRY AGAIN"
  20.             guesses += 1
  21.             print combination, guesses # Debug added by me
  22.             self.break_out()
  23.  
  24.         if guess == combination:
  25.             print "welldone"
  26.  
  27.  
  28. if __name__ == "__main__":
  29.     firstRoom()
  30.  
And if so you have an other major problem in your code. Every time you guess the combination resets as well as guesses.

edit. I got bored and fixed the program up a bit as well as extending it to make the guessing a bit more fair.

Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2.  
  3. class firstRoom():
  4.  
  5.     def __init__(self):
  6.  
  7.         print "Welcome to the start of the game. Good luck finishing it."
  8.         print """There is a keypad on the door. You have to get all 3 digits
  9.                 correct, or the door will never open again."""
  10.  
  11.         # Until everythin is working only one number
  12.         self.combination = "".join([str(randint(0,9)) for x in xrange(3)])
  13.         self.guesses = 0
  14.  
  15.     def break_out(self):
  16.  
  17.         guess = raw_input("[KEYPAD]>")
  18.  
  19.         if guess != self.combination and self.guesses < 10:
  20.  
  21.             print "INCORRECT, TRY AGAIN"
  22.             self.guesses += 1
  23.             self.eval_guess(guess)
  24.             self.break_out()
  25.  
  26.         elif guess == self.combination:
  27.             print "welldone"
  28.  
  29.         else:
  30.             print "FAIL!"
  31.  
  32.     def eval_guess(self, guess):
  33.  
  34.         # print self.guesses, self.combination # Debug
  35.  
  36.         if len(guess) == 3 and guess.isdigit():
  37.             lg = []
  38.             for x, y in zip(guess, self.combination):
  39.                 if int(x) < int(y):
  40.                     lg.append("<")
  41.  
  42.                 elif int(x) > int(y):
  43.                     lg.append(">")
  44.  
  45.                 else:
  46.                     lg.append("=")
  47.  
  48.             print "".join(lg)
  49.  
  50.  
  51. if __name__ == "__main__":
  52.     x = firstRoom()
  53.     x.break_out()

7 1972
Rabbit
12,516 Expert Mod 8TB
It shouldn't do anything. You've created a class but you never do anything with it.
Jan 27 '12 #2
So how do i get it to work?
Jan 27 '12 #3
Rabbit
12,516 Expert Mod 8TB
Instantiate a variable of the class and then use it.
Expand|Select|Wrap|Line Numbers
  1. class TestClass:
  2.    def f(self):
  3.       return 'hello world'
  4.  
  5. x = TestClass()
  6. x.f()
Jan 27 '12 #4
Thanks but it still doesn't go to the break_out function?
Jan 27 '12 #5
Rabbit
12,516 Expert Mod 8TB
We need to see the code.
Jan 28 '12 #6
bvdet
2,851 Expert Mod 2GB
As Rabbit posted, you have to call the method.
Expand|Select|Wrap|Line Numbers
  1. >>> x = firstRoom()
  2. Welcome to the start of the game. Good luck finishing it.
  3. There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again.
  4. >>> x.break_out()
  5. welldone
  6. >>>
Jan 28 '12 #7
Smygis
126 100+
I suspect that all the places you have
Expand|Select|Wrap|Line Numbers
  1. return 'break_out'
That what you really want is self.break_out()
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2.  
  3. class firstRoom():
  4.     def __init__(self):
  5.         print "Welcome to the start of the game. Good luck finishing it."
  6.         print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
  7.  
  8.         self.break_out()
  9.  
  10.     def break_out(self):
  11.  
  12.         # Until everythin is working only one number
  13.         combination = "%d" % (randint(1,3))
  14.         guesses = 0
  15.         guess = raw_input("[KEYPAD]>")
  16.  
  17.  
  18.         while guess != combination and guesses < 10:
  19.             print "INCORRECT, TRY AGAIN"
  20.             guesses += 1
  21.             print combination, guesses # Debug added by me
  22.             self.break_out()
  23.  
  24.         if guess == combination:
  25.             print "welldone"
  26.  
  27.  
  28. if __name__ == "__main__":
  29.     firstRoom()
  30.  
And if so you have an other major problem in your code. Every time you guess the combination resets as well as guesses.

edit. I got bored and fixed the program up a bit as well as extending it to make the guessing a bit more fair.

Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2.  
  3. class firstRoom():
  4.  
  5.     def __init__(self):
  6.  
  7.         print "Welcome to the start of the game. Good luck finishing it."
  8.         print """There is a keypad on the door. You have to get all 3 digits
  9.                 correct, or the door will never open again."""
  10.  
  11.         # Until everythin is working only one number
  12.         self.combination = "".join([str(randint(0,9)) for x in xrange(3)])
  13.         self.guesses = 0
  14.  
  15.     def break_out(self):
  16.  
  17.         guess = raw_input("[KEYPAD]>")
  18.  
  19.         if guess != self.combination and self.guesses < 10:
  20.  
  21.             print "INCORRECT, TRY AGAIN"
  22.             self.guesses += 1
  23.             self.eval_guess(guess)
  24.             self.break_out()
  25.  
  26.         elif guess == self.combination:
  27.             print "welldone"
  28.  
  29.         else:
  30.             print "FAIL!"
  31.  
  32.     def eval_guess(self, guess):
  33.  
  34.         # print self.guesses, self.combination # Debug
  35.  
  36.         if len(guess) == 3 and guess.isdigit():
  37.             lg = []
  38.             for x, y in zip(guess, self.combination):
  39.                 if int(x) < int(y):
  40.                     lg.append("<")
  41.  
  42.                 elif int(x) > int(y):
  43.                     lg.append(">")
  44.  
  45.                 else:
  46.                     lg.append("=")
  47.  
  48.             print "".join(lg)
  49.  
  50.  
  51. if __name__ == "__main__":
  52.     x = firstRoom()
  53.     x.break_out()
Jan 28 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Hal Vaughan | last post by:
I am working on an install program, for another program that will need a list of printers on a system. Originally I found the printers by doing this: public PrintNames() { String tName = "";...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
4
by: Zheka | last post by:
we're dealing the following problem: d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\vector(575): error C2440: 'initializing' : cannot convert from 'const Game' to 'Game' This...
9
by: Philip TAYLOR | last post by:
Configuring a new instance of IIS, I noticed that it allows an HTML-formatted document trailer to be appended to every document served. Unfortunately, on checking its behaviour, I find that it...
26
by: Simon | last post by:
I'm doing a survey. When do you think GNU/Linux will be ready for the average Joe? What obstacles must it overcome first?
4
by: serge | last post by:
I tried all the INFORMATION_SCHEMA on SQL 2000 and I see that the system tables hold pretty much everything I am interested in: Objects names (columns, functions, stored procedures, ...) stored...
5
by: Damjan | last post by:
Is there a way to see if the SELECT in cx_Oracle didn't return anything? I want to optimize the situation when the number of selected rows is zero. Is select count(*) the only option, seems...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
9
by: Ryan Liu | last post by:
Hi, I use C# wrote an Client/Server application. In production environment, will be 130 clients (Windows XP) connect to a Server (Windows 2000/2003 Server) thought TCP/IP socket in a local 100M...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.