I just want to convert minutes or days given by the user into hours. It is showing that "minute is not defined". When I print the user input, minute is getting printed, but when I want to use that "minute" given by user in the if statement, then it is showing an error.
t_in = input("Is your input in minute or hr or day? ")
print (t_in)
t = input("Enter the value: ")
if t_in == minute:
t = (1/60)*float(t)
elif t_in == day:
t = (1/24)*float(t)
print(t)
Error message :
NameError: name 'minute' is not defined
CodePudding user response:
add " around minute:
"minute","day"
CodePudding user response:
You wrote the minute without the "", which means the minute is a variable, but you don't have a minute variable. You should write the minute in "" because it is a string, not a variable.
t_in = input("Is your input in minute or hr or day? ")
print (t_in)
t = input("Enter the value: ")
if t_in == "minute":
t = (1/60)*float(t)
elif t_in == "day":
t = (1/24)*float(t)
print(t)
and you can write this code shortly, like this
t_in = input("Is your input in minute or hr or day? ")
print (t_in)
t = input("Enter the value: ")
t = (1/60)*float(t) if t_in == "minute" else t = (1/24)*float(t)
print(t)
CodePudding user response:
The issue is that you haven't actually defined the variable "minute" nor have u defined "day" as a variable. While you are equating t_into minute and elif as day, python is looking for the variable.. but you havent defined the variable and you also dont need to define..
You just need to make the minute and day a string by wrapping them by "minute" "days"
bcs t_in accepts the string value.
t = input("Enter the value: ")
if t_in == 'minute':
t = (1/60)*float(t)
elif t_in == 'day':
t = (1/24)*float(t)
print(t)
and also define a statement for 'hr' as an input for the completion of your code. if you want to :)
