#๐Ÿ”’ Need help in an if/else code

131 messages ยท Page 1 of 1 (latest)

dusky spireBOT
#

@dusky vapor

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.

dusky vapor
#

someone is already typing lol

coarse gale
#

!paste can you send your code, too?

dusky spireBOT
#
Pasting large amounts of code

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.

dusky vapor
coarse gale
#

are you defining answer before the if?

dusky vapor
#

this is the whole code

#

what am I missing

coarse gale
#

so like answer = input()

dusky vapor
verbal horizon
#

Shift back the else

#

In line with if

dusky vapor
#

I did and uh

#

diff type of error

verbal horizon
#

Show

coarse gale
#

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

dusky vapor
coarse gale
#

so when you do if answer == "something": you haven't told python what answer is.

verbal horizon
#

In your first line add answer =

#

Before the input part

dusky vapor
#

do I do

#

answer = toast

verbal horizon
#

What the error means, if you're not defining the variable answer

coarse gale
#

!e

answer = "yes"
if answer == "yes":
  print("yes")
else:
  print("no")
verbal horizon
#

So python doesn't know what answer means

dusky spireBOT
#

@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.

yes
verbal horizon
#

answer = input ()

dusky vapor
#

answer = input(Toast) ?

coarse gale
#

Where are you getting toast from?

verbal horizon
#

Nah, just add answer = to your first line at the beginning so the input gets stored inside answer

coarse gale
#

!d input

dusky spireBOT
#

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...
dusky vapor
#

is this good

verbal horizon
#

Nope

late bison
#

Your code right now is:

input("blah blah")

It should be

answer = input("blah blah"")
verbal horizon
#

answer = on the first line

#

Not second

late bison
#

Make sure you are doing answer = input("What did you eat for lunch?")

verbal horizon
#

Two other people have shared code examples please check them

dusky vapor
late bison
#

It allows you to do your if statements after.

dusky vapor
#

so the first line should be answer = input('X')

verbal horizon
#

๐Ÿ˜ญ

coarse gale
#

Delete the first line completely.

late bison
#

The What did you eat for lunch? line is the one that needs the answer in front.

dusky vapor
late bison
#

It should be

answer = input("What did you eat for lunch?")
dusky vapor
coarse gale
#

There you go.

dusky vapor
#

it should print excellent

#

not toast

verbal horizon
dusky vapor
#

if I put in toast

coarse gale
#

Then that's a different issue, but you solved the first one

#

You now defined answer, assigned something to it, and compared it.

late bison
#

What's the rest of your code?

dusky vapor
late bison
#

The second line is pointless.

#

You can delete it.

dusky vapor
#

oh shit its working

late bison
#

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.

dusky vapor
#

why should the first line begin with "'answer" = input

#

why the answer

late bison
#

answer is a variable name.

verbal horizon
#

because, you want to save what the user inputs inside answer

late bison
#

Think of it like a label on a container.

verbal horizon
#

= is used to put input() inside anwer

late bison
#

The label says answer. The container contains the input.

coarse gale
#

input returns what the user types. You have to store that somewhere.

dusky vapor
#

I forgot its a variable

late bison
#

Doing input() bare like that means nothing.

#

Usually people do it to fill lines/force the user to type stuff that isn't important.

coarse gale
#

alhtough using input() bare helps debug cli programs that just run through code, but that's a different beast.

dusky vapor
#

im learning python because im having an IT exam in 1 day

coarse gale
#

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.

late bison
#

In that case you want to force the input to a specific case.

#

.lower() for example.

coarse gale
#

for comparison, it's better to use .casefold() over .lower().

dusky vapor
#

what is casefold

late bison
#

Basically it just forces everything to lowercase.

coarse gale
#

!d str.casefold

dusky spireBOT
#

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.
coarse gale
#

!e

answer = "ToAsT"

if answer.casefold() == "toast":
  print("execellent")
else:
  print("idkdude")
dusky spireBOT
#

@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.

execellent
dusky vapor
#

what does sep='' do

coarse gale
#

!e

print("this", "is", "a", "sentence", sep="++")
dusky spireBOT
#

@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.

this++is++a++sentence
coarse gale
#

It will seperate the objects you pass into print by whatever you define as the sep

late bison
#

The default is " "

dusky vapor
#

yeah thats just a space but

#

what if I put " " between words

#

and I dont put the sep='' at the end

coarse gale
#

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="++")
dusky spireBOT
#

@coarse gale :white_check_mark: Your 3.12 eval job has completed with return code 0.

Thisisasinglestringobject
coarse gale
#

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.

slow junco
# dusky vapor

answer = str(input("what did you eat for lunch: ")) also make sure you backspace one tab so else is aligned with if

coarse gale
slow junco
#

im just jk

#

but its better that way

sage ember
#

its not better that way if it does the same thing already?

coarse gale
#

!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.

dusky spireBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

dusky vapor
#

:V

slow junco
dusky vapor
#

what is cs50

#

also

#

Jesus is God

slow junco
slow junco
dusky vapor
slow junco
#

welp i regret helping ya

dusky spireBOT
#
Python help channel closed

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