Skip to content
Advertisement

Tag: try-catch

How does int() in Python know if a number is a float?

Usually if you put int(a) or int(b) it will convert “a” and “b” into integers If I try print(int(4.5)) it will print 4 But if I try it in a try statement: Edit: If num1 = 4 and num2 = 4.5 How come Python didn’t convert num2 into an integer? Previously int() would convert a float into an integer. But

Avoid ValueError outside a while loop?

In the second line, I am trying to make it not crash if a string is entered but can’t find a way to use an exception or something similar. In the while loop it works normally as the exception deals with this case. Answer while followed by a variable condition often ends up with bugs/repeating code => bad maintenability. Use

How do I continue my try/catch in my while-loop

I’m making this blackjack game and I’m sure if you’ve played the game you know the rules. Basically I have 5 amount of chips and I’m letting the user type in their bets for the round. I have this try catch block thats supposed to not let the user type in anything below 0 and above the chip_amount. My exception

Using try and except to verify user’s input in Python

I’m doing a school project and the user will need to input some values (1, 2 or 3 and their combinations separated by comma). Ex: 2,3,1 or 2,1 I need to prevent the user from typing anything else out of the standard value. Here’s my attempt, which is working, but looks very dumb. Anyone could think somehow to improve it?

Python: Multiple try except blocks in one?

Is there a neat way to have multiply commands in the try block so that it basically tries every single line without stopping as soon as one command yields an error? Basically I want to replace this: with this: Defining a list so I could loop through the commands seems to be a bad solution Answer I’d say this is

Multiple try codes in one block

I have a problem with my code in the try block. To make it easy this is my code: Is something like this possible? Answer You’ll have to make this separate try blocks: This assumes you want to run code c only if code b failed. If you need to run code c regardless, you need to put the try

Python Try Catch Block inside lambda

Is it possible to use try catch block inside of a lambda function. I need the lambda function to convert a certain variable into an integer, but not all of the values will be able to be converted into integers. Answer Nope. A Python lambda can only be a single expression. Use a named function. It is convenient to write

Advertisement