Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework
#1
Hi,I have a homework question: Write a program that will ask for a favorite genre (type) of music, and then make a recommendation.
I have attempted it below but i don't know what went wrong.

pop=int(input("Do you like pop music?"))
alternative=int(input("Do you like alternative music?"))
indie=int(input("Do you like indie rock music?"))
soul=int(input("Do you like soul music?"))
if pop==yes:
    print("Nirvana")
elif alternative==yes:
    print("Cults")
if indie==yes:
    print("Arctic Monkeys")
elif soul==yes:
    print("The Jackson 5")
came out with this:

Error:
Traceback (most recent call last): File "main.py", line 1, in <module> pop=int(input("Do you like pop music?")) ValueError: invalid literal for int() with base 10: 'yes'
Reply
#2
Why are you trying to convert the answers to those questions to integers (which is what int does)?
Underscore likes this post
Reply
#3
(Oct-09-2021, 12:30 PM)ndc85430 Wrote: Why are you trying to convert the answers to those questions to integers (which is what int does)?
Well this is how my computing teacher taught us how to do it
Reply
#4
I doubt that, because it makes no sense. More likely is you've misunderstood what's happening. input returns a string and if you entered something like 123 at the prompt, you'd want that to be converted to an integer so you could do things that make sense for numbers (like addition, multiplication, etc.). A word like "yes" isn't an integer. You just want it to be a string, so there's no need for the calls to int.
Underscore likes this post
Reply
#5
(Oct-09-2021, 01:00 PM)chickenseizuresalad Wrote: Well this is how my computing teacher taught us how to do it

Not possible, or maybe you understood the use of int wrong.
int expects integers as str or bytes.
White spaces are stripped from left and right side automatically.
The only allowed characters depending on the base. base is by default 10, which allows the characters 0 - 9.
Base 16 (hex) allows 0 - 9, A - F.

I guess you want to use bool

def ask_yes_no(question: str) -> bool:
    question = f"{question} [y/yes|n/no]: "
    no = ["n", "no"]
    yes = ["y", "yes"]
    
    while True:
        user_input = input(question).lower().strip()
        if user_input in no:
            return False
        elif user_input in yes:
            return True
        elif not user_input:
            print("Empty answer is not allowed")
        else:
            print(f"Invalid answer: {user_input}")
This function will add the "[y/yes|n/no]: " to the question and lowers and strips the answer.
Allowed answers: y or Y for True and n or N for False.

If you have an empty str, bool("") will return False.
This is what implicit happens with the elif not user_input: part. not will negate the result of bool(user_input). So if it's empty, the result is False and the negation of it if True. An empty str will execute this branch, but in branch is no return. In this case, the while-loop will continue the looping because the condition True is always True and does not change. The only way to exit the loop is the use of return which leaves the function or break which leaves the while-loop but stays in the function.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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