Code help- absolute beginner

I’m an absolute beginner and I cannot figure out how to make it so they can choose to enter the game or not. I tried using a list but for some reason even if you type in something from the list of Yes answers it gives the answer of it not being something recognised.

https://replit.com/@lsabi1/Project2024PI-rateHeroes

This is your current code:

event = 1
Openingchoices = ["Yes","yes","Yea","yea","Yup","yup","yeah","Yeah","Sure","sure",]
Openingchoices2 = ["No","no","Nah","nah","Nope","nope",]
while event == 1:
 enterGame = input("Would you like to enter the game? \n")
 if enterGame == Openingchoices:
   print ("Welcome to π-rate Heroes! \n ")
 elif enterGame == Openingchoices2:
   print("Sad to see you go. :(")
   quit()
 else:
  print("I don't understand please try again.")
  event = 1
  quit()

The problem is that you are trying to compare a string, enterGame, to a list (such as Openingchoices). Lists are never equal to strings.
What you meant to use was to check if the string is in the list.

if enterGame in Openingchoices:

There are also many other tips and improvements. Follow the snake_case naming convention for most variables, use either 2 or 4 width space indentation, clean the user’s input, and use sys.exit() (must be imported) or let the program reach the end of the file instead of using quit().

# no need for 'event' variable because it is currently useless
yes_opening_choices = ["yes","yea","yup","yeah","sure",]
no_opening_choices = ["no","nah","nope",]
while True:
  opening_choice = input(
    "Would you like to enter the game? \n"
  ).strip().lower()  # makes it case-insensitive and ignores leading and trailing whitespace
  if opening_choice in yes_opening_choices:
    print("Welcome to π-rate Heroes!\n")
  elif opening_choice in no_opening_choices:
    print("Sad to see you go. :(")
    break  # breaks out of the current loop
  else:
    print("I don't understand please try again.")
    break  # you might not actually want to break here
5 Likes

Hi again, this time I feel like this is a really easy thing to fix, but I can’t figure out how to fix it. I want the “elif enterGame in Opening_choices_no: " to be with the " if enterGame in Opening_choices_yes:” but it seems to think it isn’t together and keeps saying expected an indented block. I’ve never had this problem before so I am wondering if I am doing anything wrong. But unfortunately, it just will not work. I was having issue with using HomeSavePoint as a way to set a point where no matter the branch you too as long as you stayed alive in the game you would link to the next part, but it was not working, so I tried to indent it. Not sure if that is right or not. It is the same game I was working on earlier in this feed. I apologize to be a bother.

Hello,
it seems that your code would benefit greatly from two changes:

  1. Since you are struggling with indentation, you should indent your code with 4 spaces per level instead of 2 or 1. (You change this near the bottom of the file, and re-indent.) This will most likely clearly show or even fix your first problem, and make your code much easier to read and understand.
  2. Learn how to use functions in your code. These are important for breaking up your code into smaller and easier to manage sections that can even be reused. (Another effect is that your main loop will be much smaller and clearer, and it reduces levels of indentation.) I’m not entirely sure what you are trying to describe in the second part of your post, but this will probably help you implement it.
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.