#๐Ÿ”’ how do i make it so if i say "Yes" or "yes" then it says the if statement instead of the else

276 messages ยท Page 1 of 1 (latest)

vapid raft
golden mistBOT
#

@vapid raft

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.

vapid raft
#

and when you input anything other than "yes" or "no" then it says something like "Please answer the question" until you say yes/no

hollow arch
#

.lower is .lower()

granite violet
#

if you want help with your code, paste it as text, not a screenshot.

#

and paste all of it, not just a little bit

hollow arch
#

looking at it, that might actually be all of it

raven laurel
#

did the responses help?

vapid raft
#

oke

fiery spear
vapid raft
fiery spear
#

you need a loop

vapid raft
#

but i need to add while loop yes?

#

i think

fiery spear
#

yes

hollow arch
fiery spear
#

any ideas?

vapid raft
raven laurel
#

while something != True

#

it should be looping

#

when the condition has been met

#

make something == true

#

that's the hint

fiery spear
#

and when the condition is false it should stop the loop

#

in your case you want to stop the loop when user has either entered a 'yes' or 'no'

#

!e

i = 5
while i > 0:
    print(i)
    i = i - 1```
golden mistBOT
#

@fiery spear :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 5
002 | 4
003 | 3
004 | 2
005 | 1
vapid raft
#

so like

fiery spear
#

see above

#

see that example to understand how to use a while loop

vapid raft
#

!e question = input("What is your name? ")
question2 = input (f"Is your name {question}? ")
while question2 == True:
if question2.lower() == ("yes"):
print("Your name is so cool!")
elif question2.lower == ("no")
break
else:
print("Please enter (Yes) or (No)")

golden mistBOT
#

@vapid raft :x: Your 3.12 eval job has completed with return code 1.

001 |   File "/home/main.py", line 6
002 |     elif question2.lower == ("no")
003 |                                   ^
004 | SyntaxError: expected ':'
winter forum
#

You don't need the () around "no"

fiery spear
vapid raft
#

how do i tell python that yes is the correct answer

fiery spear
#

when the user enters a valid answer you can change the value of a condition variable

fiery spear
golden mistBOT
#

@fiery spear :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 5
002 | 4
003 | 3
004 | 2
005 | 1
006 | 0
fiery spear
#

do you see how that works

#

I am changing the value of variable run_until_0 to False when i = 0

#

you can do something similar when user has entered yes or no you can change some condition variable to False

vapid raft
#

i do not understand it

fiery spear
#

wait

winter forum
vapid raft
#

is anyone available to call so i can screenshare

#

and try explaining it to me like im a new born

fiery spear
#

nah my introverted ass heads out

winter forum
#

Maybe somebody else might but I don't do that, sorry.

vapid raft
#

all good!

fiery spear
fiery spear
vapid raft
#

I dont understand how to make it loop after it asks the second question

fiery spear
#

while run_until_0 = True
This line checks if run_until_0 is True

#

if it is True then run the code inside the while loop

#

if it's false then don't run the code inside the loop

vapid raft
#

so

#

while question2 == True:

#

in my head

#

should run the question on repeat

fiery spear
#

make it while True

#

while True:

#

now run the code

#

what happens when you enter a wrong answer

#

u there?

#

@vapid raft

vapid raft
#

it spams

#

Please Enter (Yes) or (No)

fiery spear
#

ok

vapid raft
#
question2 = input (f"Is your name {question}? ")
while True:
    if question2.lower() == "yes":
        print("Your name is so cool!")
    elif question2.lower == "no":
        break
    else:
        print("Please enter (Yes) or (No)")```
fiery spear
vapid raft
#

every time the user enters an answer that is not

#

yes or no

steep veldt
fiery spear
steep veldt
#

question2 = input("Please enter (Yes) or (No)")

#

put this in the else box instead

#

see what happens

fiery spear
#

ye

vapid raft
#

whenever i type no

#

it says please enter yes or no

#

and whenever i press yes

#

it infinitely says your name is so cool

fiery spear
#

question2.lower()

#

you have question2.lower

#

you need ()

#

delete that

steep veldt
#

also add a break after "yes"

fiery spear
#

don't spoonfeed

steep veldt
#

oki

vapid raft
#

i added () after the second question2.lower

#

and it broke the code without giving a message

steep veldt
#

post the code you have right now

fiery spear
vapid raft
#

the no works

#
question2 = input (f"Is your name {question}? ")
while True:
    if question2.lower() == "yes":
        print("Your name is so cool!")
    elif question2.lower() == "no":
        print("You lied to me!")
        break
    else:
        question2 = input("Please enter (Yes) or (No) ")```
steep veldt
#

add a break in the yes one

#

the way that while True: loops work is that you have to stop them yourself

vapid raft
#

whenever i say yes though, it infinitely runs your name is so cool!

steep veldt
#

we want else: to update whatever is in the loop which we do by using input()

vapid raft
#

so i have to break the yes or it will run infinitely?

steep veldt
#

add a break

#

mhm

fiery spear
#

yes

vapid raft
#

and i have to break after the no as well or it will run infinitely?

fiery spear
#

yes

vapid raft
#

and then the else statement

#

makes it restart the code

fiery spear
#

runs infinitely until you answer yes or no

vapid raft
#

whenever i put in something that isnt "yes" or "no"

steep veldt
#

exactly

#

while True loops are very handy

vapid raft
#

i think it works!

steep veldt
#

awesome

vapid raft
#
question = input("What is your name? ")
question2 = input (f"Is your name {question}? ")
while True:
    if question2.lower() == "yes":
        print("Your name is so cool!")
        break
    elif question2.lower() == "no":
        print("You lied to me!")
        break
    else:
        question2 = input("Please enter (Yes) or (No) ")```
steep veldt
#

oh yeah

#

this thing^^

#

add a py after ```

#

makes the code all colorful

#

splendid

fiery spear
#

but usually break statement is not recommended, there are better ways to do the same

steep veldt
#

out of curiosity how would you do it @fiery spear

vapid raft
steep veldt
#

since i would link the while loop to some sort of variable most of the time

vapid raft
#

is there a way to shorten it?

steep veldt
#

not really

fiery spear
vapid raft
#

also is there a way to make it so .lower() doesnt show on each and every single one

#

like can i use a nested loop to make it so everything in the nested loop is lower?

fiery spear
vapid raft
#

from what i learned a nested loop is a loop in a loop

steep veldt
#

I would do

success = False
while not success:
  if condition:
    success = True
  else:
    (code)```
vapid raft
#

yeah i dont know that

winter forum
#

It's a while loop with an if/else inside of it

steep veldt
#

anything nested is broadly a "thing" inside of a "thing"

winter forum
#

What MildlyPanda showed you wasn't a nested loop.

steep veldt
#

a dictionary inside a dictionary

#

a function inside a function

#

etc etc

winter forum
#

a dream within a dream

steep veldt
#

nested dreams

fiery spear
#

lol

#

nested MildlyPanda

steep veldt
#

be me
self-reflection
find nested me

#

ok i think the question has been anwered here

#

off i go

vapid raft
#

i dont know the terms dictionary or function

#

but i think i get the general jist of how while loops work

#

now what is a for loop

steep veldt
#

This course will give you a full introduction into all of the core concepts in python. Follow along with the videos and you'll be a python programmer in no time!

Click the โš™๏ธ to change to a dub track in Spanish, Arabic, or Portuguese, or Hindi.
(Hindi dubbed via Melt Labs - https://www.withmelt.com/)

Want more from Mike? He's starting a codin...

โ–ถ Play video
#

this is the one i used, but feel free to play it at x2 speed since 4 hours is a little bit daunting

vapid raft
#

i dont like how he stares at me

steep veldt
#

slowing it down and rewinding in case you didnt catch something

vapid raft
#

but ill give it a go

steep veldt
#

dont worry there will be plenty of online sources filled with heaps of knowledge and computer nerds staring at you when you gain experience as a coder

vapid raft
#

i think i might be stuck on for loops for a little bit

steep veldt
#

alright ill quickly explain for loops

vapid raft
#

someone explained them to me

#

but i dont understand them

steep veldt
#

for loops accept many kinds of variables

#

strings, lists, and dictionaries for example

#

and it goes through every "element" of that variable

#

for example, in a string, the string is comprised off multiple letters

#

when you put that string in a for loop

vapid raft
#

what is a dictionary

#

a list is like this [1, 2, 3, 4, 5] right?

#

a string is ("text")

steep veldt
#

you get to assign another variable to each individual element

steep veldt
steep veldt
steep veldt
vapid raft
#

is a dictionary

word = 1

#

the dictionary is word?

steep veldt
#

nope

#

a dictionary is defined by curly brackets

#
dictionary = {}```
#

and inside of dictionaries you have a SINGLE key

#

and a corresponding value

#

for example 'yes' is my key

#

'nice' is my value

vapid raft
#

okay so a dictionary is like

#

word = 1

#

print(f"I love {word} apple")

steep veldt
#
dictionary = {
  'yes': 'nice'
}```
#

in this case it would be

#

dictionary['yes'] (which would give us 'nice')

vapid raft
#

i am confused

steep veldt
#

dont worry

#

watch the video and he'll give a more thorough explanation

#

or at least i would hope so if youre sitting through it for 4 hours

#

ok yeah for now just watch the video

vapid raft
#

Aye aye capn'

steep veldt
#

and save all your questions until after you've finished watching it

vapid raft
#

I'll see how far into the video I can get

steep veldt
#

you might not think that everything in the video is necessary

#

but almost EVERYTHING

vapid raft
#

I'm playing lethal with some friends in an hour or two so I don't think I'll have time to finish it tonight

steep veldt
#

i have eventually found a use for

#

np np

#

in your own time

vapid raft
#

i = 0

#

what is i

#

0 is an integer right?

#

what is i called in that situation

steep veldt
#

i is an integer right

#

i is a variable, and it's type is an integer

#

on the other hand

#

if i did

#

i = "0"

vapid raft
#

so variable =

steep veldt
#

i is a string

#

yes

vapid raft
#

so if i did

#

i = True

steep veldt
#

= is always when you want to set a variable

vapid raft
#

that means

#

i variable = True

steep veldt
#

True/False is what programmers call a boolean

steep veldt
#

all you need is

#

i = True

vapid raft
#

yes

#

but like

steep veldt
#

and then i will be created as a variable

vapid raft
#

i = True

#

in that situation

#

i is a variable

steep veldt
#

yes

vapid raft
#

and its boolean is true

steep veldt
#

until it gets replaced by something else

#

yes

vapid raft
#

so

#

i = 2

#

i = [1, 2, 3, 4, 5]

#

is a list

steep veldt
#

yes

vapid raft
#

variable i = that list

#

so print(i) would print that list

#

print is used to print a variable or string?

steep veldt
#

print can be used to print anything

vapid raft
#

so print

#

prints

steep veldt
#

mhm

vapid raft
#

input makes something that you can reply to?

steep veldt
#

in the case of stuff like strings or integers it wont tell you what type the printed variable is

steep veldt
vapid raft
#

and print would make the input visible

steep veldt
#

input() just "freezes" the code until it gets a response

vapid raft
#

some guy ranted to me and dipped

vapid raft
#

to print the alphabet

#

and stop at a certain letter

#

how would i do that

#

can you call so i can screenshare and say my thought process?

steep veldt
#

so you want to input the letter that is stops at?

vapid raft
#

you dont have to vc or use mic

steep veldt
#

sure

#

let me set up a google meet

#

where i live discord is blocked

#

is that okay with you?

vapid raft
#

Sure

golden mistBOT
#
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.