0

I have a question. So I want to use the input() function in python, but whatever variable you input in the input when it runs, I want to be assigned as a variable and then you can use it as a calculation later. For example,

variable = input("What is your model? ")
array = [1,2,3,4]
k1234 = array[0]
math = str(variable) + 5

That's just a simple example of what I am trying to do. But when I run it, and when I get the prompt: "What is your model?", I type in k1234 but then when it tries to do the math calculation it throws me the error:

ValueError: invalid literal for int() with base 10: 'k1234'

In total, I basically have an array of 20 - 25 values, and have a variable assigned to each of the indices separately in the array, but when you type the variable in the input section, it doesn't recognize it as a integer later on and can't perform the calculation. Anyone have any suggestions? I don't have much experience with input().

Edit: The question "How can I read inputs as integers?" doesn't quite answer my question. I'm trying to get a function that you put a string into the input, will recognize the string as a variable that is defined later on to then be recognized as an integer which you can then use as a calculation later on. :)

martineau
  • 119,623
  • 25
  • 170
  • 301
kneesarethebees
  • 85
  • 1
  • 2
  • 10
  • 1
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – Bill the Lizard Jun 08 '18 at 17:11
  • I looked over that article, and it was helpful! It wasn't quite what I was asking but it definitely helped with my understanding of how the input function works. Thanks! – kneesarethebees Jun 08 '18 at 17:34

1 Answers1

0
variable = input("What is your model? ")
array = [1,2,3,4]
k1234 = array[0]
math = str(variable) + 5

This is not working because when you ask for the user's input and the user types "k123", they are setting "variable" to the string "k123". If you check the contents of "variable" you will see that it is 'k123'.

Another reason why this program doesn't work is because you are trying to add a string (variable) to an integer (5). To fix both problems, you may want to do something like this:

variable = input("What is your model? ") #sets the user's input to variable
array = [1,2,3,4]              #defining a list
if variable == "k1234":        #checks the contents of "variable"
    variable = array[0]        #if variable=="k1234" is true, this sets variable to the first item in the list array (which is 1)
math = int(variable) + 5       #int(variable) makes sure that the contents of this variable is an integer, then it adds the result with 5 and sets the answer to the variable "math".
print(math)

If you run this program, then the result will be 6.

CU12
  • 54
  • 7
  • Oh okay, that makes total sense. I'm going to do that. So what if I have like 20 variables that I want set to the specific index? Like I have an array of 20 values, but each value is equal to a new variable (e.g, k1234 = 1, k12345 = 2, k123456 = 3, etc.) Will I have to do an if statement for all of those variables defined by that array? For example: if variable == "k1234": variable = array[0] elif variable == "k12345": variable = array[1] ? – kneesarethebees Jun 08 '18 at 17:31