user7345804
user7345804

Reputation:

Allowing user to choose an input

I am trying to sharpen my python programming skills by coding solutions to basic physics problems.

The problem states that a spaceship is traveling a distance x at a velocity v towards a destination from Earth. I am to find the time elapsed by a stationary observer on Earth and the time elapsed by a passenger aboard the spaceship. Rather than assigning values to x and v, I am to allow the user to input values for x and v. But, I would like to allow the user to choose how they would like to input their data. For example, the user could put v = some number in meters/sec or the user could put v = a*c where 0 ≤ a ≤ 1 and c = speed of light; I would like to let the user decide which input is preferred.

I understand how to ask the user to input a value. ex: x = float(input("What distance has the spaceship traveled: "))

But how can I let the user decide which v they would like to input?

Upvotes: 0

Views: 104

Answers (1)

akraf
akraf

Reputation: 3255

Possiblility 1 : Two questions

input(...) returns a string. Therefore you can ask the user which input style is preferred, à la

Which input format is preferred? ([a]bsolute/[f]raction of c) >

Then use an if/else block to test whether the user has entered a or f.

Subsequently, ask another question

Enter the desired speed (number) >

and convert the input to float just like you have done above (float(input(...)))

Possibility 2

You can ask a question like

Enter spaceship speed:

and test if the last character which is entered by the user is a c. Then you use the number as fraction of light speed. Else, you use the number as absolute speed.

To get the last character of a string, use

s = 'abc'
lastcharacter = s[-1]

Upvotes: 1

Related Questions