Hi,
I'm new to Python and OO in general. I am trying to set up a simple loop based around an inputed answer to a question, so that if you input a wrong answer, you are asked the question again, but if correct, the program moves on.
For example:
What is the capital of France?
Correct answer - move on to next question. Wrong answer. Repeat the question
I used to write in basic years ago in which programs had line numbers and would have went something like this. It was easy to direct the program back again to ask the question again.
10 print "what is the capital of France?"
20 get a$ #user inputs answer
30 If A$ = "Paris" then goto 60
40 print "That is the wrong answer!. Try again"
50goto 10
60 The next question........
I'm really finding it hard to get my head round loops without line numbers.
thanks,
tim
16 1939 bvdet 2,851
Expert Mod 2GB
Hi,
I'm new to Python and OO in general. I am trying to set up a simple loop based around an inputed answer to a question, so that if you input a wrong answer, you are asked the question again, but if correct, the program moves on.
For example:
What is the capital of France?
Correct answer - move on to next question. Wrong answer. Repeat the question
I used to write in basic years ago in which programs had line numbers and would have went something like this. It was easy to direct the program back again to ask the question again.
10 print "what is the capital of France?"
20 get a$ #user inputs answer
30 If A$ = "Paris" then goto 60
40 print "That is the wrong answer!. Try again"
50goto 10
60 The next question........
I'm really finding it hard to get my head round loops without line numbers.
thanks,
tim
Try this: -
z=1
-
while z:
-
answer = ....ask for input...
-
if answer in [...list of possible correct answers...]:
-
break
-
else:
-
print "You have entered an incorrect answer. Try again."
-
Read yesterday's post "need help with code structure" for pythonic structure on how to do this.
Thanks for the replies. I've solved most of the problem. Can someone tell me why this dooes not work
namestring = input("WHAT IS YOUR NAME?" )
while namestring == 'Jack':
age = 9
while namestring == 'Rachel':
age = 7
I'm trying to set a value for age depending on what name was entered.
Also, is there a way to make Python ignore upper,lower case or a command along th same lines as
while namestring = 'Jack' or 'JACK' or 'jack':
age=9
thanks
the age is indented by the way. It just did not show on the message
tim
the age is indented by the way. It just did not show on the message
tim
That's because you didn't USE CODE TAGS! The background of the post screen has it, the panel on the right of the post screen has it, the sticky at the top of this forum has it. Read them. Use them. It helps us a lot.
Thanks for posting, keep it up. Use code tags. Thanks,
Barton
Thanks for the replies. I've solved most of the problem. Can someone tell me why this dooes not work
namestring = input("WHAT IS YOUR NAME?" )
while namestring == 'Jack':
age = 9
while namestring == 'Rachel':
age = 7
I'm trying to set a value for age depending on what name was entered.
Also, is there a way to make Python ignore upper,lower case or a command along th same lines as
while namestring = 'Jack' or 'JACK' or 'jack':
age=9
thanks
while creates a loop which needs a condition that changes (to "no longer true") or a break statement in order to break out of the loop. -
namestring = raw_input("WHAT IS YOUR NAME? " )
-
namestring = namestring.captalize() # use str type method to get a capitalized copy of namestring
-
if namestring == 'Jack':
-
age = 9
-
if namestring == 'Rachel':
-
age = 7
-
type
help(str)
to see all the things that strings know how to do.
Thanks for all the help, but one last question!!!
Now, even if the name is entered correctly and assigned to namestring, the age value is not altered.
Any ideas?
Thanks,
Tim
I had given age an initial value age = 0 at the start of the program, but this isn't in the loop by the way
tim
I had given age an initial value age = 0 at the start of the program, but this isn't in the loop by the way
tim
Post you program. Be sure to use code tags. It's probably a matter of scope. The scope of a variable (global or local) depends on where/how you declare/assign it. -
aGlobal = 100
-
def func():
-
aLocal = 100
-
func()
-
print aLocal
-
Traceback (most recent call last):
-
File "<pyshell#0>", line 1, in -toplevel-
-
print aLocal
-
NameError: name 'aLocal' is not defined
-
See what I mean?
Hi,
This is the code. I was just playing around with a simple program that would amuse kids and would teach me about using
simple loops.
the age value always seems to be 0. I would also like a bit of code to ask the name again if the wrong name was input.
thanks!!! -
from time import sleep
-
age = 0
-
print 'hello'
-
sleep(5)
-
print 'I bet I can tell whether you are telling the truth about how old you are ?'
-
sleep(5)
-
print 'I CAN prove it to you'
-
sleep(5)
-
print 'You still dont believe me, do you?'
-
namestring = raw_input("WHAT IS YOUR NAME?" )
-
namestring = namestring.capitalize()
-
if namestring == 'Jack':
-
age = 9
-
if namestring == 'Rachel':
-
age = 7
-
answer = 0
-
while answer != age:
-
answer=input ("What is your age? ")
-
if answer != age:
-
print "YOU ARE FIBBING"
-
print 'NOW YOU ARE TELLING THE TRUTH'
-
By the way, I think the names should be capitalized, but there is no difference when i do this.
t
By the way, I think the names should be capitalized, but there is no difference when i do this.
t
The code ran for me. It looks good too. Try this: -
from time import sleep
-
age = 0
-
print 'hello'
-
sleep(5)
-
print 'I bet I can tell whether you are telling the truth about how old you are ?'
-
sleep(5)
-
print 'I CAN prove it to you'
-
sleep(5)
-
print 'You still dont believe me, do you?'
-
namestring = raw_input("WHAT IS YOUR NAME?" )
-
### debug using print
-
print namestring
-
### debug using print
-
namestring = namestring.capitalize()
-
### debug using print
-
print namestring
-
### debug using print
-
if namestring == 'Jack':
-
age = 9
-
if namestring == 'Rachel':
-
age = 7
-
### debug using print
-
print namestring, age
-
### debug using print
-
answer = 0
-
while answer != age:
-
answer=input ("What is your age? ")
-
if answer != age:
-
print "YOU ARE FIBBING"
-
print 'NOW YOU ARE TELLING THE TRUTH'
-
Using print statements is a really good way to debug your code.
Thanks for all the help. You've been patient!!!
Heres something funny. I copied your code with debug points into a new window, saved it as a new file and ran it. It worked fine. Then I took all of your debug points out and ran it again. It worked perfectly.
Then I loaded my original code and it wouldn't run, yet line by line it looks exactly the same. I can't explain that I'm afraid :-)
Thanks again
p.s. I'm only getting back into programming after a LOT of years away. Back then the Commodore Vic-20 was king and programmers could manage to write games like frogger with its 'huge' 5K (3.5K useable) of free memory :-) I could actually churn out a reasonable program myself believe it or not!!!!
If you do't mind being patient once more, can this be simplified :-
if namestring != 'Rachel' and namestring != 'Jack' and namestring != '':
If you do't mind being patient once more, can this be simplified :-
if namestring != 'Rachel' and namestring != 'Jack' and namestring != '':
I don't mind at all. That's what we're here for.
This can be simplified in a very cool and extensible way using a list: -
people = ['Jack', 'Rachel']
-
if namestring not in people:
-
# do stuff for unknown people
-
The in operator is the same a writing a function like this: -
def IsInList(key, list): # Return True if key is in list
-
for item in list:
-
if key == item:
-
return True
-
in works on most iterators (lists, dictionaries, file objects, etc) in all sorts of cool ways.
I guess that you know what not does.
I don't mind at all. That's what we're here for.
This can be simplified in a very cool and extensible way using a list: -
people = ['Jack', 'Rachel']
-
if namestring not in people:
-
# do stuff for unknown people
-
The in operator is the same a writing a function like this: -
def IsInList(key, list): # Return True if key is in list
-
for item in list:
-
if key == item:
-
return True
-
in works on most iterators (lists, dictionaries, file objects, etc) in all sorts of cool ways.
I guess that you know what not does.
The equivalent function above uses the in operator (I do it without thinking) as a generator so that we don't write functions like this: -
for i in range(len(aList)): # There's in again, can't get away from it!
-
yeild aList[i]
-
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JustSomeGuy |
last post by:
I have a need to make an applicaiton that uses a variable number of nested
for loops.
for now I'm using a fixed number:
for (z=0; z < Z; ++z)
for (y=0; y < Y; ++y)
for (x=0; x < X; ++x)
|
by: newtothis |
last post by:
I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :
int i ;
for(i = 0 ; i < 4 ; i++)
{
|
by: Gonzalo Aguirre |
last post by:
i have three classes like this
class foo{
attrib_1;
..
..
public:
..
|
by: cppsks |
last post by:
Hello. I posted a question regarding this yesterday. I came up with the
following solution but I am a little hesistant as to this solution having
any side-effects that I am not aware of. The...
|
by: Neptune |
last post by:
Hello.
I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
(Sam's series), and for nested loops, he writes (p116) "It's often
necessary to create a loop even when you are...
|
by: Peter Olcott |
last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in...
|
by: Crirus |
last post by:
I have this situation
25 26 ........
9 10 11 12 13
24 1 2 3 15
23 8 x 4 15
22 7 6 5 16
21 20 19 18 17
|
by: Kunal |
last post by:
Hello,
I need help in removing if ..else conditions inside for loops. I have
used the following method but I am not sure whether it has actually
helped.
Below is an example to illustrate what I...
|
by: ertis6 |
last post by:
Hi all,
I need to calculate a value inside 8 nested for loops. 2 additional
for loops are used during calculation. It was working fine with 4
loops. My code is like this:
...
for(int i1=0;...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |