Python Forum
Mixed string,Integer input variable issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixed string,Integer input variable issue
#1
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.
Reply
#2
Use a list instead of names containing numbers
choices = ['Choice_C', 'Choice_B', 'Choice_A']
...
print(choices[line-1])
Reply
#3
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}>')
Output:
λ python line_code.py pick a line number: Line_2 Choice_B -------------- Input was <Line_2> with result of <Choice_B> λ python line_code.py pick a line number: Line1000 No record of that input -------------- Input was <Line1000> with result of <No record of that input>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Commas issue in variable ddahlman 6 368 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Using string input for boolean tronic72 3 682 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,120 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Replacing String Variable with a new String Name kevv11 2 769 Jul-29-2023, 12:03 PM
Last Post: snippsat
  input variable choice MCL169 7 1,155 Feb-19-2023, 09:00 PM
Last Post: MCL169
  Need help on how to include single quotes on data of variable string hani_hms 5 2,001 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,776 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 952 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,257 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,758 Jul-27-2022, 02:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020