Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
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.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2012, 02:09 PM
Jack2311 Jack2311 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 30 Jack2311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Thanks

Reply With Quote
  #2  
Old February 7th, 2012, 02:21 PM
totalknowledge totalknowledge is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 54 totalknowledge User rank is Sergeant (500 - 2000 Reputation Level)totalknowledge User rank is Sergeant (500 - 2000 Reputation Level)totalknowledge User rank is Sergeant (500 - 2000 Reputation Level)totalknowledge User rank is Sergeant (500 - 2000 Reputation Level)totalknowledge User rank is Sergeant (500 - 2000 Reputation Level) 
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.

Reply With Quote
  #3  
Old February 7th, 2012, 03:18 PM
Jack2311 Jack2311 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 30 Jack2311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old February 7th, 2012, 04:43 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
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

Reply With Quote
  #5  
Old February 8th, 2012, 06:45 AM
Jack2311 Jack2311 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 30 Jack2311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to Loop this program?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap