#πŸ”’ While would spam the terminal despite the conditions

35 messages Β· Page 1 of 1 (latest)

magic nimbus
#

When I receive a prompt to enter a number between 1 and 3, and I input any of those numbers, the terminal would repeatedly display the message 'Please choose a number between 1 and 3.' Could you explain why this behavior occurs? Thanks!

def load():
    conversion_question = int(input("Enter 1 to convert USD to MXN\n\
Enter 2 to convert MXN to USD\n\
Enter 3 to terminate"))
    while True:
        if conversion_question not in {1 or 2 or 3}:
            print("Please choose between 1 and 3")
        elif conversion_question == 1 or conversion_question == 2:
            return currency_conversion()
        elif conversion_question == 3:
            print("Come back soon!")
            break
patent sluiceBOT
#

@magic nimbus

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.

whole marlin
#

How many times will that input line be reached?

#

Also, not in {1 or 2 or 3} is not valid. That's actually the first time I've ever seen someone try and write it like that. You mean not in {1, 2, 3}.

magic nimbus
#

That was what I was looking for, thanks very much

whole marlin
#

It will still loop infinitely if you enter bad input though.

#

Unless you move input into the loop.

magic nimbus
honest coral
#

also, there may be an issue if they enter a non-integer value

magic nimbus
#

It would output traceback yeah

whole marlin
#

input needs to be in the loop so you can ask the user for corrected input. Otherwise, the same bad input gets checked in the first condition over and over.

honest coral
#

I typically do input validation like this:

while True:
  a = input("Here are your options:\n1.\n2.\n3.\Enter an option as a number:") #use .lower() for textual words if you choose to use them
  if a not in ("1","2","3"): #checks to see if the input is a valid option
    print("Invalid input")
    continue #If not it runs this code and repeats from this point
  break #breaks from the loop or you can delete this to run other code
magic nimbus
#

Noted, thank you very much πŸ™

whole marlin
#

(Although that continue is unnecessary in this case).

honest coral
#

Yeah, you could do

while True:
  a = input("Here are your options:\n1.\n2.\n3.\Enter an option as a number:") #use .lower() for textual words if you choose to use them
  if a in ("1","2","3"): #checks to see if the input is a valid option
    break #breaks from the loop or you can delete this to run other code
  print("Invalid input")  
magic nimbus
#

I just realized that conversion_question is not defined in this another def function, but I want to use a variable defined inside this function from the other function I mentioned above

def currency_conversion():
    exchange_rate = float(input("Enter the exchange rate: \n"))
    if conversion_question == 1:
        usd_amount = float(input("Enter USD amount: "))
        usd_conversion = exchange_rate * usd_amount
        print(usd_conversion)
    elif conversion_question == 2:
        mxn_amount = float(input("Enter MXN amount: "))
        mxn_conversion = exchange_rate / mxn_amount
        print(mxn_conversion)
#

is there guideline i could find?

#

Ohh I have to declare the variable to be global?

whole marlin
#

That's what parameters and return are for. return the data from load, then pass it in as a parameter to the other function.

whole marlin
#

Use parameters to supply functions with data as long as that's practical.

#

!return

patent sluiceBOT
#
Return statement

A value created inside a function can't be used outside of it unless you return it.

Consider the following function:

def square(n):
    return n * n

If we wanted to store 5 squared in a variable called x, we would do:
x = square(5). x would now equal 25.

Common Mistakes

>>> def square(n):
...     n * n  # calculates then throws away, returns None
...
>>> x = square(5)
>>> print(x)
None
>>> def square(n):
...     print(n * n)  # calculates and prints, then throws away and returns None
...
>>> x = square(5)
25
>>> print(x)
None

Things to note

  • print() and return do not accomplish the same thing. print() will show the value, and then it will be gone.
  • A function will return None if it ends without a return statement.
  • When you want to print a value from a function, it's best to return the value and print the function call instead, like print(square(5)).
whole marlin
#

!param

patent sluiceBOT
#
Parameters vs. arguments

A parameter is a variable defined in a function signature (the line with def in it), while arguments are objects passed to a function call.

def square(n): # n is the parameter
    return n*n

print(square(5)) # 5 is the argument

Note that 5 is the argument passed to square, but square(5) in its entirety is the argument passed to print

magic nimbus
#

I dont think I follow πŸ˜…

#

So basically I would need to add a value inside a function as in def square(this)?

whole marlin
patent sluiceBOT
#
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.