#πŸ”’ Sorting Hat Program

55 messages Β· Page 1 of 1 (latest)

worn narwhal
#

I was attempting to make the code return an error if the incorrect value was entered as an answer, then ask the question again. I then realised it just jumped to the next question and i have no clue how to make it re-ask the question

hollow salmonBOT
#

@worn narwhal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

worn narwhal
#

oh it said no images

#

uhh mb

#

patient = input("Would you describe yourself as being patient?: ").lower()
if patient=="yes":
hufflepuff = hufflepuff + 1
elif patient=="no":
slytherin = slytherin + 1
else:
print("Invalid input. Please enter yes or no.")

hollow salmonBOT
#

Hey @worn narwhal!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
worn narwhal
#

oh what

#

cool

#

'''py
patient = input("Would you describe yourself as being patient?: ").lower()
if patient=="yes":
hufflepuff = hufflepuff + 1
elif patient=="no":
slytherin = slytherin + 1
else:
print("Invalid input. Please enter yes or no.")
'''

#

huh

tired elbow
#

its

#

`

#

not '

worn narwhal
#

oh

tired elbow
#

beside the num 1 key

worn narwhal
#

`

#

coolty

#
patient = input("Would you describe yourself as being patient?: ").lower()
if patient=="yes":
  hufflepuff = hufflepuff + 1 
elif patient=="no":
  slytherin = slytherin + 1 
else:
    print("Invalid input. Please enter yes or no.")
#

WHOA

#

awesome

#

anyways

#

how can i make it reask the question

tired elbow
#

you need a loop

worn narwhal
#

and a break under the if and elif

tired elbow
#

yes

worn narwhal
#

ew

#

okok

#

that seems unecessarily long

#

but ok

tired elbow
#

you could write a function for it

worn narwhal
#

thanks

#

okok

tired elbow
#

you should

worn narwhal
#

lol

tired elbow
#

"dry" code is good code

#

"Don't Repeat Yourself"

worn narwhal
#

okok thanks

shell flax
#

This: slytherin = slytherin + 1
can be written: slytherin += 1
which avoids repeating the variable name.

worn narwhal
#

yeah i realised that after i wrote the first few

#

so i js went with it

#

fixedd

shell flax
#

Probably worth writing a yesno(prompt) function which issues the prompt using input(), checks for a yes/no and returns True or False accordingly, and complains for other inputs (and maybe asks again, and again).

worn narwhal
#

well

shell flax
#

Then you can go:

if yesno("Would you describe yourself as patient?: "):
    hufflepuff += 1
else:
    slytherin += 1
worn narwhal
#

i wrote the code, it works how i want it to, its 110 lines somy teacher will count it so i should be alr

#

ohhh

#

thats

#

yeah

#

i think the teacher will complain if i start using stuff that we have not learnt yet

shell flax
#

This is why functions are nice. You can put that same repeated ogic in one .

#

Maybe so.

worn narwhal
#

he complained when i used if statements in the first unit

#

but thank you for your help

hollow salmonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.