- from random import randint
-
-
class firstRoom():
-
def __init__(self):
-
print "Welcome to the start of the game. Good luck finishing it."
-
print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
-
return 'break_out'
-
-
def break_out(self):
-
-
# Until everythin is working only one number
-
combination = "%d" % (randint(1,3))
-
guesses = 0
-
guess = raw_input("[KEYPAD]>")
-
-
-
while guess != combination and guesses < 10:
-
print "INCORRECT, TRY AGAIN"
-
guesses += 1
-
return 'break_out'
-
-
if guess == combination:
-
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.
I suspect that all the places you have
That what you really want is self.break_out() -
from random import randint
-
-
class firstRoom():
-
def __init__(self):
-
print "Welcome to the start of the game. Good luck finishing it."
-
print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
-
-
self.break_out()
-
-
def break_out(self):
-
-
# Until everythin is working only one number
-
combination = "%d" % (randint(1,3))
-
guesses = 0
-
guess = raw_input("[KEYPAD]>")
-
-
-
while guess != combination and guesses < 10:
-
print "INCORRECT, TRY AGAIN"
-
guesses += 1
-
print combination, guesses # Debug added by me
-
self.break_out()
-
-
if guess == combination:
-
print "welldone"
-
-
-
if __name__ == "__main__":
-
firstRoom()
-
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. - from random import randint
-
-
class firstRoom():
-
-
def __init__(self):
-
-
print "Welcome to the start of the game. Good luck finishing it."
-
print """There is a keypad on the door. You have to get all 3 digits
-
correct, or the door will never open again."""
-
-
# Until everythin is working only one number
-
self.combination = "".join([str(randint(0,9)) for x in xrange(3)])
-
self.guesses = 0
-
-
def break_out(self):
-
-
guess = raw_input("[KEYPAD]>")
-
-
if guess != self.combination and self.guesses < 10:
-
-
print "INCORRECT, TRY AGAIN"
-
self.guesses += 1
-
self.eval_guess(guess)
-
self.break_out()
-
-
elif guess == self.combination:
-
print "welldone"
-
-
else:
-
print "FAIL!"
-
-
def eval_guess(self, guess):
-
-
# print self.guesses, self.combination # Debug
-
-
if len(guess) == 3 and guess.isdigit():
-
lg = []
-
for x, y in zip(guess, self.combination):
-
if int(x) < int(y):
-
lg.append("<")
-
-
elif int(x) > int(y):
-
lg.append(">")
-
-
else:
-
lg.append("=")
-
-
print "".join(lg)
-
-
-
if __name__ == "__main__":
-
x = firstRoom()
-
x.break_out()
7 1972
It shouldn't do anything. You've created a class but you never do anything with it.
So how do i get it to work?
Instantiate a variable of the class and then use it. - class TestClass:
-
def f(self):
-
return 'hello world'
-
-
x = TestClass()
-
x.f()
Thanks but it still doesn't go to the break_out function?
bvdet 2,851
Expert Mod 2GB
As Rabbit posted, you have to call the method. - >>> x = firstRoom()
-
Welcome to the start of the game. Good luck finishing it.
-
There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again.
-
>>> x.break_out()
-
welldone
-
>>>
I suspect that all the places you have
That what you really want is self.break_out() -
from random import randint
-
-
class firstRoom():
-
def __init__(self):
-
print "Welcome to the start of the game. Good luck finishing it."
-
print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
-
-
self.break_out()
-
-
def break_out(self):
-
-
# Until everythin is working only one number
-
combination = "%d" % (randint(1,3))
-
guesses = 0
-
guess = raw_input("[KEYPAD]>")
-
-
-
while guess != combination and guesses < 10:
-
print "INCORRECT, TRY AGAIN"
-
guesses += 1
-
print combination, guesses # Debug added by me
-
self.break_out()
-
-
if guess == combination:
-
print "welldone"
-
-
-
if __name__ == "__main__":
-
firstRoom()
-
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. - from random import randint
-
-
class firstRoom():
-
-
def __init__(self):
-
-
print "Welcome to the start of the game. Good luck finishing it."
-
print """There is a keypad on the door. You have to get all 3 digits
-
correct, or the door will never open again."""
-
-
# Until everythin is working only one number
-
self.combination = "".join([str(randint(0,9)) for x in xrange(3)])
-
self.guesses = 0
-
-
def break_out(self):
-
-
guess = raw_input("[KEYPAD]>")
-
-
if guess != self.combination and self.guesses < 10:
-
-
print "INCORRECT, TRY AGAIN"
-
self.guesses += 1
-
self.eval_guess(guess)
-
self.break_out()
-
-
elif guess == self.combination:
-
print "welldone"
-
-
else:
-
print "FAIL!"
-
-
def eval_guess(self, guess):
-
-
# print self.guesses, self.combination # Debug
-
-
if len(guess) == 3 and guess.isdigit():
-
lg = []
-
for x, y in zip(guess, self.combination):
-
if int(x) < int(y):
-
lg.append("<")
-
-
elif int(x) > int(y):
-
lg.append(">")
-
-
else:
-
lg.append("=")
-
-
print "".join(lg)
-
-
-
if __name__ == "__main__":
-
x = firstRoom()
-
x.break_out()
Sign in to post your reply or Sign up for a free account.
Similar topics
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 = "";...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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++ ) {
|
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...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |