Python Forum

Full Version: Str/Int variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm very beginner trying to learn to code and I'm stuck with this... I need to convert strings to integers (to use it in plots), but I want users to be able to define them as "letters".

Example from code:

import matplotlib.pyplot as plt

mon = 0
tue = 1
wen = 2

beg1 = input("Starting day? mon/tue/wen")
peop1 = int(input("Number of people?"))

course1=(beg1, peop1)
For the first question, I would like user to be able to write e.g. "mon" and for the second e.g. "3" and then I would get course1 = (0, 3) and then plot it, but I get Value error to first question ("Could not convert string to float..."). Could anybody help me, what is wrong in code?

..and now I got it working using "eval" - problem solved.
I think it would be better for you to use a dictionary. For example:

days = {"mon": 0, "tue": 1, "wen": 2}
 
beg1 = input("Starting day? mon/tue/wen")
peop1 = int(input("Number of people?"))
 
course1=(days[beg1], peop1)
Using eval is generally discouraged, see for example:
https://stackoverflow.com/questions/1832...d-practice