#🔒 lists and while loops
47 messages · Page 1 of 1 (latest)
@stone grotto
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 share the code in text and maybe use a pastebin service?
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.
Reading a code screenshot is not good for the eyes
also, what is the problem with it? Are you getting any error messages?
So instead of a while loop, you can use recursive function call and inside a try/except
def get_nummin_num: int, prompt: str) -> int:
user_input = input (prompt)
while (user_ input.isdigit()) or (user_input < min_num) :
print('your entery was not valied try again')
else:
print(user_input)
I don’t know if I did that right but that’s my code
Are you still allowed to use while?
Yes just not while true for some reason
wdym?
are you allowed to use while or not?
I am trying to get it to ask for a number input and if it is a str or below zero to repeat asking until the input is above 0
No my prof said I have to use while but no while not
ok...
Then use while True
Ok I’ll try that is there anything else I need to fix. For when the input is a str
Use a try/except
If its not an int, it throws a ValueError exception
Use a flag variable
flag = False
while not flag:
inp = input()
if inp.isditgit() and inp < min_number:
flag = True
His prof said no while not #1294209917863067679 message
thats such a roundabout way of doing this. Could just leave it as while True and when the if conditions are met, use break
Point is, put the input() into the while loop and set it to run the first time
You put the input() line outside of the loop so it will not print the prompt for the user again because it is dead lock
It keep checking if input is ditgit and bigger than min number but user never have a chance to type it because there's no prompt to because the prompt is outside the loop
Where would I put the except value error for str then?
try:
int(input())
except ValueError:
continue
Ok that all works but now it’s giving me an error that says. < not supported between an str and a int
That would be a different exception
-except ValueError:
+except (ValueError, TypeError):
Where do I put that in the try part?
Do you know how try/except works?
No I don’t think I’ve learnt that yet
Ok thanks
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.