BigAl
BigAl

Reputation: 21

How to loop back to initial variable input in python?

I am new to programming, so bear with me. I want to create a program that combines whole numbers and fractions. It also displays their metric equivalents. While I am sure there is a better way to do this, I have done it as follows:

**from __future__ import division
w=int(input('Enter Whole Number: '))
fn=int(input('Enter Fraction Numerator: '))
fd=int(input('Enter Fraction Denominator: '))
print w+(fn/fd)," inches"
print (w+fn/fd)*25.4," mm"**

My question is how do I get the program to prompt the user with "Enter whole number:" every time he/she reaches the end of the program? Thanks.

Upvotes: 1

Views: 331

Answers (1)

Anthony Pham
Anthony Pham

Reputation: 3106

Simply use a while loop:

**from __future__ import division
while True:                                 #Never ending
    w=int(input('Enter Whole Number: '))
    fn=int(input('Enter Fraction Numerator: '))
    fd=int(input('Enter Fraction Denominator: '))
    print w+(fn/fd)," inches"
    print (w+fn/fd)*25.4," mm"**

Or use a function and while loop:

**from __future__ import division
def Function():
    w=int(input('Enter Whole Number: '))
    fn=int(input('Enter Fraction Numerator: '))
    fd=int(input('Enter Fraction Denominator: '))
    print w+(fn/fd)," inches"
    print (w+fn/fd)*25.4," mm"**

while True:
    Function()

To quit, how about making the input equal to "QUIT" then quit the program or use break to stop the loop thus ending the program.

Upvotes: 1

Related Questions