#🔒 try/except

123 messages · Page 1 of 1 (latest)

thick storm
#

why erore ?

knotty estuaryBOT
#

@thick storm

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.

strange grail
#

Can you paste the full error you're getting?

hardy iris
#

Do you get an error when you run it?

cold shale
#

@thick storm show your error

thick storm
#

@cold shale

lusty reef
final lintel
#

Well yeah…

thick storm
#

@lusty reef

final lintel
#

If your try except fails

#

Then you didn’t define n

thick storm
final lintel
#

You need to actually set n to something if you’re gonna use it

lusty reef
#

If the exception is caught, n never gets defined

thick storm
#

?

cold shale
#

Just except everything

lusty reef
#

Put the code inside the try

heady sage
#

Or you can just do exit() in the except clause

cold shale
#
try:
  something()
except Error as e:
  print(e)
#

If I was right

final lintel
#

Or…
Just do
while True:
N = input()
Try
N =int(N)
Break

floral cove
# thick storm hoow
while True:
    try:
        user_input = int(input(' > '))
        break
    except ValueError:
        print('Input must be a valid number, try again')
final lintel
#

👍

thick storm
final lintel
#

You just need some sort of loop

#

To keep asking the user

floral cove
thick storm
#

what make break

heady sage
#

!d break

knotty estuaryBOT
#

7.9. The break statement


break_stmt ::=  "break"
``` [`break`](https://docs.python.org/3/reference/simple_stmts.html#break) may only occur syntactically nested in a [`for`](https://docs.python.org/3/reference/compound_stmts.html#for) or [`while`](https://docs.python.org/3/reference/compound_stmts.html#while) loop, but not nested in a function or class definition within that loop.

It terminates the nearest enclosing loop, skipping the optional `else` clause if the loop has one.

If a [`for`](https://docs.python.org/3/reference/compound_stmts.html#for) loop is terminated by [`break`](https://docs.python.org/3/reference/simple_stmts.html#break), the loop control target keeps its current value...
floral cove
# thick storm what make break

and if an exception is raised (if user didnt enter a valid number), break is not executed, it goes into except ValueError:, prints 'Input must be a valid number, try again', and loop starts over

floral cove
#

no problem

thick storm
#

@floral cove

#

n now work

#

NameError: name 'n' is not defined

floral cove
#

n doesnt exist in your program

#

and lower you still did for i in range(n):

thick storm
#

ty brooo

thick storm
#

@floral cove

floral cove
#

?

thick storm
#

im need some help ((:

floral cove
#

yeah sure

#

what happened

thick storm
#

There is one mistake in the book that repeats it

#

Not everything repeats itself

floral cove
#
def input_float(promt='', error_message=''):
    while True:
        try:
            user_input = float(input(promt))
            return user_input
        except ValueError:
            print(error_message)


# ... (your main part)
name = input(...)
grade = input_float(f"enter the mark '{name}': ", "Please enter a valid float number")
# ...
thick storm
#

ok 1m

thick storm
floral cove
#

?

#
def input_float(promt='', error_message=''):
    while True:
        try:
            user_input = float(input(promt))
#

you give promt to this function

#

and that input() will print it out

#
input_float(f"enter the mark '{name}': ", ...
thick storm
#

@floral cove

#

not work

floral cove
#

show ur code

thick storm
#

@floral cove

floral cove
#

why did u paste in the comment 💀

thick storm
#

💀

floral cove
#

i meant to change ur for loop

floral cove
#

also u can write a function like that

#
>>> def input_type(promt='', error_message='', _type=int):
...     while True:
...         try:
...             user_input = _type(input(promt))
...             return user_input
...         except ValueError:
...             print(error_message)
...
>>> input_type('enter a float: ', 'input must be a float', float)
enter a float: 12
12.0
>>> input_type('enter an int: ', 'input must be int', int)
enter an int: 100
100

which u can use in both places where u need int and float

thick storm
#

OMG

#

its hard

thick storm
floral cove
#

u'll get there

floral cove
# thick storm

now just delete the comment and edit ur actual loop that u needed to change

thick storm
#

whu _type=int will make ??

#

@floral cove

floral cove
#

u do int(input(...)) right

#

but int is sort of a function so u can store it in a variable

#
a = int
value = a('50')
print(type(value))

and we get

<class 'int'>
#

so we can create an argument _type that will store this function

#
>>> input_type('enter a float: ', 'input must be a float', float)
enter a float: 12
12.0
>>> input_type('enter an int: ', 'input must be int', int)
enter an int: 100
100
#

look here

#

the third argument in first call is float

#

and we get back 12.0

#

which is a float

#

and in second call we use int

#

and we get back 100

#

which is an integer

thick storm
thick storm
floral cove
#

so yeah

#

_type=int means that the default value of _type is int

#

if we dont use it it will be int

thick storm
#

I understand now

#

and why you use

#

float: 12 and int: 100

#

@floral cove

floral cove
floral cove
#

and i got back an integer

thick storm
#

oh okkk

floral cove
#

if u will change the function to input_type like i showed before

#
>>> def input_type(promt='', error_message='', _type=int):
...     while True:
...         try:
...             user_input = _type(input(promt))
...             return user_input
...         except ValueError:
...             print(error_message)
...
#

then in ur code

floral cove
# thick storm how im can make it herer
for i in range(user_input):
    name = #...(same as before)
    grade = input_type(f"enter the mark '{name}': ", "Please enter a valid float number", float)
    student_grades[name] = grade
floral cove
thick storm
#

I don't understand what is written, can you explain?

floral cove
#

what exactly u dont understand

thick storm
thick storm
#

The break will work whether it is true or not

knotty estuaryBOT
#
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.