我希望用户能够继续使用该程序。但我仍然粗略地使用 while 循环
I want the User to be able to continue using the program. But Im still Sketchy using while loop
print'Free fall distance and velocity calculator'
g=9.81
def finalvelocity(t):
vf=g*t
return vf
def height(t):
h=0.5*g*t
return h
t=input('Enter the value of time in seconds: ')
print'What do you want to get?'
print'[1]. Final Velocity'
print'[2]. Height'
choice= input('Enter Selected Number: ')
if choice==1:
print 'Answer is', finalvelocity(t),'meter per second'
if choice==2:
print 'Answer is', height(t), 'meters'
if choice>2:
print 'Invalid Selection'
if choice<1:
print 'Invalid Selection'
for choice in range(choice>2):
n=raw_input('Do you want to continue?[y]yes/[n]no: ')
while True:
t=input('Enter the value of time in seconds: ')
print'What do you want to get?'
print'[1]. Final Velocity'
print'[2]. Height'
choice= input('Enter Selected Number: ')
if choice==1:
print 'Answer is', finalvelocity(t),'meter per second'
if choice==2:
print 'Answer is', height(t), 'meters'
if choice>2:
print 'Invalid Selection'
if choice<1:
print 'Invalid Selection'
if n==n:
break
感谢您更新您的问题。您可以使用无限循环一次又一次地 运行 程序,直到用户希望退出。一个超级简单的例子是:
while True:
t = raw_input("Do you want to exit? y/n: ")
if t == 'y':
break
else:
print "You stayed!"
至于您的代码,您可以按照以下方式使其 运行 无限次。请注意,如果用户选择 'No',代码 break
将如何退出循环以退出程序。
print 'Free fall distance and velocity calculator'
g = 9.81
def finalvelocity(t):
vf = g*t
return vf
def height(t):
h = 0.5*g*t
return h
# Start an infinite loop of user input until they exit
while True:
# Ask for time
t = input('Enter the value of time in seconds: ')
# Ask for query type
print'What do you want to get?'
print'[1]. Final Velocity'
print'[2]. Height'
choice= input('Enter Selected Number: ')
# Perform calculations
if choice==1:
print 'Answer is', finalvelocity(t), 'meter per second'
if choice==2:
print 'Answer is', height(t), 'meters'
else:
print 'Invalid Selection'
# Offer the user a chance to exit
n = raw_input('Do you want to calculate another? [y]yes/[n]no: ')
if n == 'n':
# User wants to exit
break # Break out of the infinite loop
print'Free fall distance and velocity calculator'
g=9.81
def finalvelocity(t):
vf=g*t
return vf
def height(t):
h=0.5*g*t
return h
t=input('Enter the value of time in seconds: ')
print'What do you want to get?'
print'[1]. Final Velocity'
print'[2]. Height'
choice= input('Enter Selected Number: ')
if choice==1:
print 'Answer is', finalvelocity(t),'meter per second'
if choice==2:
print 'Answer is', height(t), 'meters'
if choice>2:
print 'Invalid Selection'
if choice<1:
print 'Invalid Selection'
for choice in range(choice>2):
n=raw_input('Do you want to continue?[y]yes/[n]no: ')
while True:
t=input('Enter the value of time in seconds: ')
print'What do you want to get?'
print'[1]. Final Velocity'
print'[2]. Height'
choice= input('Enter Selected Number: ')
if choice==1:
print 'Answer is', finalvelocity(t),'meter per second'
if choice==2:
print 'Answer is', height(t), 'meters'
if choice>2:
print 'Invalid Selection'
if choice<1:
print 'Invalid Selection'
if n==n:
break
感谢您更新您的问题。您可以使用无限循环一次又一次地 运行 程序,直到用户希望退出。一个超级简单的例子是:
while True:
t = raw_input("Do you want to exit? y/n: ")
if t == 'y':
break
else:
print "You stayed!"
至于您的代码,您可以按照以下方式使其 运行 无限次。请注意,如果用户选择 'No',代码 break
将如何退出循环以退出程序。
print 'Free fall distance and velocity calculator'
g = 9.81
def finalvelocity(t):
vf = g*t
return vf
def height(t):
h = 0.5*g*t
return h
# Start an infinite loop of user input until they exit
while True:
# Ask for time
t = input('Enter the value of time in seconds: ')
# Ask for query type
print'What do you want to get?'
print'[1]. Final Velocity'
print'[2]. Height'
choice= input('Enter Selected Number: ')
# Perform calculations
if choice==1:
print 'Answer is', finalvelocity(t), 'meter per second'
if choice==2:
print 'Answer is', height(t), 'meters'
else:
print 'Invalid Selection'
# Offer the user a chance to exit
n = raw_input('Do you want to calculate another? [y]yes/[n]no: ')
if n == 'n':
# User wants to exit
break # Break out of the infinite loop