Getting a bad input error, not sure why.


Joined
Sep 14, 2022
Messages
2
Reaction score
0
So this is just the bare beginning of a program that's supposed to calculate either the days passed in the month or the year, depending on the user's choice. My problem is in the choice() function, I'm getting a bad input error on the second if statement. Ive tried changing it to an elif, and an else, and I'm getting the same error. Anyone know how to fix this?

Python:
#define functions
def choice(c):
    if c == 1:
        #funton for days passed in month
    if c == 2:
        print("lol")
        #fucntion for days passed in year

def leap_year(year):
    if year/4 == 0:
        return 1
    if year/4 != 0:
        return 0
   

#main
print("Please enter a date")
day = input("Day: ")
month = input("Month: ")
year = ("Year: ")

print("Menu:")
print("1) Calculate the number of days in the given month.")
print("2) Calculate the number of days passed in the given year")
choice = input()
choice(choice)
 
Ad

Advertisements

Joined
Sep 18, 2022
Messages
1
Reaction score
0
I'm not able to run your code anyway because you defined choice twice .

First, you do this:
Code:
>>> def choice(c):
...     if c == 1:
...         print("lolol")
...         #funton for days passed in month
...     elif c == 2:
...         print("lol")
...         #fucntion for days passed in year
...
>>> type(choice)
<class 'function'>

Then you redefine choice as a str:

Code:
>>> choice = input()
1
>>> type(choice)
<class 'str'>

So I rewrote the whole thing:
Python:
#define functions
def calculate(choice, date):
    if choice == "1":
        print("Choice 1")
        # code for days passed in a month
    elif choice == "2":
        print("Choice 2")
        # code for days passed in a year

def leap_year(year):
    if year/4 == 0:
        return 1
    if year/4 != 0:
        return 0
  

def main(choice):
    my_date = {}
    print("Please enter a date.")
    my_date["day"] = input("Day: ")
    my_date["month"] = input("Month: ")
    my_date["year"] = input("Year: ")
    calculate(choice, my_date)

if __name__ == "__main__":
    print("Menu:")
    print("1) Calculate the number of days in the given month.")
    print("2) Calculate the number of days passed in the given year")
    choice = input("Which would you like to do?\n")
    
    main(choice)
 
Ad

Advertisements

Joined
May 11, 2022
Messages
51
Reaction score
3
i think your problem is the end code line: choice = input()
you have a function named choice, so this assignment overwrites that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top