2017-05-17 6 views
0
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 
+0

実際の質問は何ですか?プログラムは何をしていますか、それはどうすべきか、そうすべきではないと思いますか? – Gricey

+0

私は、ユーザーが終了しない限り、プログラムの使用を継続できるようにしたいと考えています。しかし、私はまだwhileループを使用してよく知られていません。誰かが私が無限ループを望むなら、whileループを使用するように提案しましたが、私のプログラムにそれをどのように統合するのか分かりません。 –

+0

プログラムは決定文を持つ行まで動作しますが、このプログラムは一方向でしか動作せず、別の入力を試したい場合はプログラムを再実行する必要があります。私は、ユーザーがプログラムをやり直さずに続けて使用できるようにしたい。 –

答えて

0

ご質問ありがとうございます。無限ループを使用して、ユーザーが終了するまでプログラムを何度も実行することができます。これの超簡単な例は次のようになります:

while True: 
    t = raw_input("Do you want to exit? y/n: ") 
    if t == 'y': 
     break 
    else: 
     print "You stayed!" 

あなたのコードについては、ここでは無限に実行する方法を示します。ユーザが 'いいえ'を選択した場合、コードを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 
+0

改変をありがとう、私はこのプログラムを試し、多分いくつかの機能を追加します。これは非常に大きな助けになりました! –

+0

心配はいりません。あなたがその答えに満足しているなら、それを受け入れることを忘れないでください – Gricey

関連する問題