-1

I'm very new to python I've just started my first year at university, I need to write a program that prompts the user to input a value E.G "AMS 0 25 S E" and then potentially using indexing, split each of the individual values and turn them into variables which I can use in calculations.

I essentially want the user to be able to put in a flight specification and have the program make calculations and true false statements based on the input values.

a = input("Please Enter your flight specification: ") b = a.split(" ")

  • Welcome to SO! I think your question is too vague: It's not clear how the expected output should look like. Please show what you have tried so far and then ask questions related to your code. – Timus Nov 03 '20 at 12:54

2 Answers2

0

It would be difficult to create variables at runtime. But you could create an array and have named variables to hold the indexes, then you can manipulate the input with the same level of flexibility but a syntax just a little bit different :

input = "AMS 0 25 S E"
arr = input.split()

varA = 0
varB = 1
varC = 2
varD = 3
varE = 4

print(arr[varC]) #=> 25
dspr
  • 2,383
  • 2
  • 15
  • 19
0

I understand the problem like this: We have a userinput and want to split it at certain separators.

For this you can use str.split:

a = "AMS 0 25 S E"
b = a.split(" ")
print (b)
for elem in b:
    print (elem)

Output:

['AMS', '0', '25', 'S', 'E']

AMS 
0 
25 
S 
E
Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
  • Yeah that's along the lines of what I wanted except I don't want to print the split values, I want to use them in other calculations and true false statements – The_maestro3 Nov 03 '20 at 12:22
  • Well that is no problem, the whole input is accessible through b and you can simply index them like b[0], b[1] etc. – Mailerdaimon Nov 03 '20 at 12:34
  • is there a way I can apply the split but to a float or integer instead? – The_maestro3 Nov 03 '20 at 17:17
  • You can convert string input to float or integer: https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int – Mailerdaimon Nov 04 '20 at 05:55