#🔒 try/except
123 messages · Page 1 of 1 (latest)
@thick storm
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.
Can you paste the full error you're getting?
Do you get an error when you run it?
@thick storm show your error
Use ValueError instead of NameError.
Well yeah…
im nake it
You need to actually set n to something if you’re gonna use it
If the exception is caught, n never gets defined
Just except everything
Put the code inside the try
Or you can just do exit() in the except clause
Or…
Just do
while True:
N = input()
Try
N =int(N)
Break
ok 1m
while True:
try:
user_input = int(input(' > '))
break
except ValueError:
print('Input must be a valid number, try again')
now work
👍
why use break ??
break ends the while True loop
what make break
!d break
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...
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
oh okkkkkkk
ty for help
no problem
because variable that stores input is user_input
n doesnt exist in your program
and lower you still did for i in range(n):
ty brooo
@floral cove
?
im need some help ((:
how im can make it herer
There is one mistake in the book that repeats it
Not everything repeats itself
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")
# ...
ok 1m
put why you put promt im input
?
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}': ", ...
show ur code
why did u paste in the comment 💀
💀
i meant to change ur for loop
this one
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
It's hard for a 15 year old to understand this
u'll get there
now just delete the comment and edit ur actual loop that u needed to change
well u see
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
bake
yup
oh okkkkkkk
I know this
so yeah
_type=int means that the default value of _type is int
if we dont use it it will be int
those are just examples
>>> input_type('enter an int: ', 'input must be int')
enter an int: 10
10
also as u can see i didnt specify int as the third argument
and i got back an integer
oh okkk
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
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
and also u can change the previous loop to
user_input = input_type(' > ', 'Input must be a valid number, try again', int)
I don't understand what is written, can you explain?
what exactly u dont understand
It's hard for me😭
This code is the first thing that will be in it. If the code is correct, it will work. If it is incorrect, it will not work. We have recovered from the fracture in both. If the value is true, it will work if it is false. Will not work.
The break will work whether it is true or not
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.