Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Str/Int variables
#1
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.
Reply
#2
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
Reply


Forum Jump:

User Panel Messages

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