Discuss How to Loop this program? in the Python Programming forum on Dev Shed. How to Loop this program? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 30
Time spent in forums: 6 h 40 m 4 sec
Reputation Power: 1
How to Loop this program?
I have an assignment that states...
Create a condition looop that will ask the user for input of two numbers. The numbers should be added and the sum displayed. The loop should also ask the user if he or she wishes to perform the operation again. If so, the loop should repeat, otherwise the loop should terminate.
This is what I have com up with...
n1=input('Please enter your first number: ')
print "You have entered the number ",n1,""
n2=input('Pleae enter your second number: ')
print "You have entered the number ",n2,""
total=n1+n2
print "I will now add your numbers together."
print "The result is:",total
y = raw_input('Would you like to run the program again? y=yes n=no')
print'The program will now terminate.'
y='y'
while y=='y':
print 'The program will start over.'
When you run this the first part of the program will work but when it ask you to run again it will continuously state "The program will start over."
How do I allow for the user to input wether or not they would like to start the program over and how do I word this so that it will loop?
Posts: 54
Time spent in forums: 22 h 36 m 41 sec
Reputation Power: 7
Your while loop is printing without a break because this is the only part of the program that is being executed.
while y=='y':
print 'The program will start over.'
You need the entire portion of code that you want repeated inside the while loop, and inside the while loop must be a condition that can set the value of y to something other than 'y' or the loop will never end.
Posts: 30
Time spent in forums: 6 h 40 m 4 sec
Reputation Power: 1
Quote:
Originally Posted by totalknowledge
Your while loop is printing without a break because this is the only part of the program that is being executed.
while y=='y':
print 'The program will start over.'
You need the entire portion of code that you want repeated inside the while loop, and inside the while loop must be a condition that can set the value of y to something other than 'y' or the loop will never end.
Could you please demonstrate what you meen. I am new to this stuff and do not understand your response.
Thanks
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
He meant this
Code:
'''
Create a condition looop that will ask the user for input of two
numbers. The numbers should be added and the sum displayed. The loop
should also ask the user if he or she wishes to perform the operation
again. If so, the loop should repeat, otherwise the loop should
terminate.
This is what I have come up with...
'''
repeat = 'y'
while 'y' == repeat: # at first sight repeat == 'y'
# indented code is the body of the loop
n1=input('Please enter your first number: ')
print "You have entered the number ",n1,""
n2=input('Pleae enter your second number: ')
print "You have entered the number ",n2,""
total=n1+n2
print "I will now add your numbers together."
print "The result is:",total
# the user can change repeat's value here!
repeat = raw_input('Would you like to run the program again? y=yes n=no ')
# display the message only when asked to repeat.
if 'y' == repeat:
print 'The program will start over.'
# indentation to outer level.
# This statement executes when repeat is no longer 'y'.
# causing the loop to exit and the program continues .
print'The program will now terminate.'
Can you improve the program? Investigate program responses to invalid second numbers entered as
'some pig'
if
n1
gesundheit
When posting python code, please use code tags. The indentation is critical.
hilite the code with your mouse, then click # sign icon or the php icon.
Thanks.
Last edited by b49P23TIvg : February 7th, 2012 at 04:44 PM.
Reason: n1 ... I intended n1 entry for number 2
Posts: 30
Time spent in forums: 6 h 40 m 4 sec
Reputation Power: 1
Quote:
Originally Posted by b49P23TIvg
Code:
'''
Create a condition looop that will ask the user for input of two
numbers. The numbers should be added and the sum displayed. The loop
should also ask the user if he or she wishes to perform the operation
again. If so, the loop should repeat, otherwise the loop should
terminate.
This is what I have come up with...
'''
repeat = 'y'
while 'y' == repeat: # at first sight repeat == 'y'
# indented code is the body of the loop
n1=input('Please enter your first number: ')
print "You have entered the number ",n1,""
n2=input('Pleae enter your second number: ')
print "You have entered the number ",n2,""
total=n1+n2
print "I will now add your numbers together."
print "The result is:",total
# the user can change repeat's value here!
repeat = raw_input('Would you like to run the program again? y=yes n=no ')
# display the message only when asked to repeat.
if 'y' == repeat:
print 'The program will start over.'
# indentation to outer level.
# This statement executes when repeat is no longer 'y'.
# causing the loop to exit and the program continues .
print'The program will now terminate.'
Can you improve the program? Investigate program responses to invalid second numbers entered as
'some pig'
if
n1
gesundheit
When posting python code, please use code tags. The indentation is critical.
hilite the code with your mouse, then click # sign icon or the php icon.
Thanks.
I see. That is very helpful the way you posted that with the code labeld.
Thanks until better paid.