#๐ Need help in an if/else code
131 messages ยท Page 1 of 1 (latest)
@dusky vapor
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.
someone is already typing lol
!paste can you send your code, too?
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
are you defining answer before the if?
so like answer = input()
where am I supposed to put that
Show
so, you are getting user input, and you want to store it somewhere
you're also checking if the answer is something, but not telling python what to store in answer
so when you do if answer == "something": you haven't told python what answer is.
What the error means, if you're not defining the variable answer
!e
answer = "yes"
if answer == "yes":
print("yes")
else:
print("no")
So python doesn't know what answer means
@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.
yes
answer = input(Toast) ?
Where are you getting toast from?
Nah, just add answer = to your first line at the beginning so the input gets stored inside answer
!d input
input()``````py
input(prompt)```
If the *prompt* argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, [`EOFError`](https://docs.python.org/3/library/exceptions.html#EOFError) is raised. Example:
```py
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
``` If the [`readline`](https://docs.python.org/3/library/readline.html#module-readline) module was loaded, then [`input()`](https://docs.python.org/3/library/functions.html#input) will use it to provide elaborate line editing and history features.
Raises an [auditing event](https://docs.python.org/3/library/sys.html#auditing) `builtins.input` with argument `prompt` before reading input...
Nope
Your code right now is:
input("blah blah")
It should be
answer = input("blah blah"")
Please check my message above.
Make sure you are doing answer = input("What did you eat for lunch?")
Two other people have shared code examples please check them
why should this be the very first code
This sets the answer to whatever the user puts in.
It allows you to do your if statements after.
๐ญ
Delete the first line completely.
yes.
The What did you eat for lunch? line is the one that needs the answer in front.
bro I suck ass at python
It should be
answer = input("What did you eat for lunch?")
There you go.
Nw, just gotta try
if I put in toast
Then that's a different issue, but you solved the first one
You now defined answer, assigned something to it, and compared it.
What's the rest of your code?
oh shit its working
It should work, yes.
This is because you have made some crucial changes to your code.
The biggest change was the fact that you assigned the variable answer to the input.
This allows the user to type in their input and the code will be able to access the input.
answer is a variable name.
because, you want to save what the user inputs inside answer
Think of it like a label on a container.
= is used to put input() inside anwer
The label says answer. The container contains the input.
input returns what the user types. You have to store that somewhere.
Doing input() bare like that means nothing.
Usually people do it to fill lines/force the user to type stuff that isn't important.
alhtough using input() bare helps debug cli programs that just run through code, but that's a different beast.
im learning python because im having an IT exam in 1 day
now remember that python is case-sensitive, so toast, Toast, and TOAST are all different. So, if the user types any variation than what you're comparing to, you'll get the else statement.
ye ik
for comparison, it's better to use .casefold() over .lower().
what is casefold
A more aggressive version of .lower()
Basically it just forces everything to lowercase.
!d str.casefold
str.casefold()```
Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.
Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter `'ร'` is equivalent to `"ss"`. Since it is already lowercase, [`lower()`](https://docs.python.org/3/library/stdtypes.html#str.lower) would do nothing to `'ร'`; [`casefold()`](https://docs.python.org/3/library/stdtypes.html#str.casefold) converts it to `"ss"`.
The casefolding algorithm is [described in section 3.13 โDefault Case Foldingโ of the Unicode Standard](https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf).
New in version 3.3.
!e
answer = "ToAsT"
if answer.casefold() == "toast":
print("execellent")
else:
print("idkdude")
@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.
execellent
what does sep='' do
!e
print("this", "is", "a", "sentence", sep="++")
@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.
this++is++a++sentence
It will seperate the objects you pass into print by whatever you define as the sep
The default is " "
yeah thats just a space but
what if I put " " between words
and I dont put the sep='' at the end
if you don't pass sep an argument, it defaults to space. But, that's only for each object you pass.
So, a single string won't use sep, no matter what you pass it.
!e
print("This" "is" "a" "single" "string" "object", sep="++")
@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.
Thisisasinglestringobject
That line concatenates to a single str() object.
When you separate each with a comma, you are sending multiple str() objects. Then sep will be applied.
answer = str(input("what did you eat for lunch: ")) also make sure you backspace one tab so else is aligned with if
input() always returns a string, no need to cast it to str().
yea but its cooler that way
im just jk
but its better that way
its not better that way if it does the same thing already?
!res @dusky vapor you can check out our resource page for lots of learning material. A lot of beginners like Automate the boring stuff, and the byte of python. Both are great starting material.
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
:V
cs50 is good
edx
keep that to yourself
John 10:30
welp i regret helping ya
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.
๐ Need help in an if/else code