![]() |
Mixed string,Integer input variable issue - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Mixed string,Integer input variable issue (/thread-13907.html) |
Mixed string,Integer input variable issue - maderdash - Nov-06-2018 The issue I am having is that when I input a number [1-3] It is accepted but I can not get it to be viewed as a variable. So it will print "Line_2" but it will not activate "Line_2" and print "Choice_B" which is what I need it to do. Now this of course just a test script for a much bigger script, but It is failing just the same way. If I replace [print(Line_2)] then the output is"Choice_B" as it should be. So I must be missing a conversion in the variables. Any help with this would be much appreciated. Line_1 = "Choice_C" # I would like to choose one of these lines Line_2 = "Choice_B" # I would like to choose one of these lines Line_3 = "Choice_A" # I would like to choose one of these lines line = input("pick a line number:") # I would enter the number 1-3 line = int(line) # I wastrying to change this to an interger. print("line_" + str(line)) # then I tried changeing it to a string. # This is the output when running pick a line number: 2 line_2 The issue is I need the printed output to say: Choice_B When I select number 2. RE: Mixed string,Integer input variable issue - Gribouillis - Nov-06-2018 Use a list instead of names containing numbers choices = ['Choice_C', 'Choice_B', 'Choice_A'] ... print(choices[line-1]) RE: Mixed string,Integer input variable issue - snippsat - Nov-06-2018 Other option is to make a dictionary,can just use your code and add dict() .Also showing f-string so can avoid code like "line_" + str(line) .# Make dictionary line_record = dict( Line_1 = "Choice_C", Line_2 = "Choice_B", Line_3 = "Choice_A", ) line = input("pick a line number: ") result = line_record.get(line, 'No record of that input') print(result) print('--------------') # Example of f-string print(f'Input was <{line}> with result of <{result}>')
|