Jump to content

Need Python failsafe help

Go to solution Solved by madknight3,

This should help

 

What's the canonical way to check for type in python?

Python - Determine the type of an object?

 

There's also the saying "it's better/easier to ask for forgiveness than permission". Some people like to apply this to coding in certain situations. Basically, work with the information as if it was correct and use a try/except statement to handle when things go wrong.

I'm making a program for fun and I need a way to make the program loop back if the input from the user is a string instead of an integer which is what the program is expecting, Is there any way to have an if statement like:

if (input = string):

 print "this is not an option"

 

and have the whole thing in a while loop so that it loops back to the beginning?

CP: Ryzen 7 2700x | Motherboard AsRock B450M | RAM 32GB Corsair ddr4 | GPU EVGA (RIP) 1070ti | Case Corsair Vengeance C-70 Black | Storage 512gb Samsung NVME ssd | PSU EVGA 850w Platinum | Display Asus PB258Q 1440p

| Cooling Noctua NH D15 | Keyboard Cooler master mechanical kb with blue switches | Mouse Logitech G502 | Sound Beyerdynamic DT 990

| Laptop Maingear Vector Pro 2 (Ryzen 9 6900xh, Nvidia 3070t, 32gb ram)

 

 

Link to comment
https://linustechtips.com/topic/338533-need-python-failsafe-help/
Share on other sites

Link to post
Share on other sites

This should help

 

What's the canonical way to check for type in python?

Python - Determine the type of an object?

 

There's also the saying "it's better/easier to ask for forgiveness than permission". Some people like to apply this to coding in certain situations. Basically, work with the information as if it was correct and use a try/except statement to handle when things go wrong.

Link to post
Share on other sites

This should help

 

What's the canonical way to check for type in python?

Python - Determine the type of an object?

 

There's also the saying "it's better/easier to ask for forgiveness than permission". Some people like to apply this to coding in certain situations. Basically, work with the information as if it was correct and use a try/except statement to handle when things go wrong.

 

 

Exceptions are your friend:

 

def getIntInput(message):  try:    value = int(input(message))  except ValueError:    value = getIntIntput("Please enter an integer: ")  return value

Right I see, I was not aware of this function before so thanks for the help guys!

CP: Ryzen 7 2700x | Motherboard AsRock B450M | RAM 32GB Corsair ddr4 | GPU EVGA (RIP) 1070ti | Case Corsair Vengeance C-70 Black | Storage 512gb Samsung NVME ssd | PSU EVGA 850w Platinum | Display Asus PB258Q 1440p

| Cooling Noctua NH D15 | Keyboard Cooler master mechanical kb with blue switches | Mouse Logitech G502 | Sound Beyerdynamic DT 990

| Laptop Maingear Vector Pro 2 (Ryzen 9 6900xh, Nvidia 3070t, 32gb ram)

 

 

Link to post
Share on other sites

Exceptions are your friend:

 

def getIntInput(message):  try:    value = int(input(message))  except ValueError:    value = getIntIntput("Please enter an integer: ")  return value

hey umm, I made the function like this:

 
def GetRowInput(message):
    try:
        row = int(message)
    except ValueError:
        row = GetRowInput("Please enter an integer: ")

 

 

and in the actual code I called it as this:

 

row = poozhikala_module.GetRowInput("Please enter the row of the seat you would like to purchase: ")

 

But it ends up giving me this error:

 

  File "G:\personal\python\homework\hw 6 and 7\poozhikala_main.py", line 29, in <module>
    row = poozhikala_module.GetRowInput("Please enter the row of the seat you would like to purchase: ")
  File "G:\personal\python\homework\hw 6 and 7\poozhikala_module.py", line 86, in GetRowInput
    row = GetRowInput("Please enter an integer: ")

CP: Ryzen 7 2700x | Motherboard AsRock B450M | RAM 32GB Corsair ddr4 | GPU EVGA (RIP) 1070ti | Case Corsair Vengeance C-70 Black | Storage 512gb Samsung NVME ssd | PSU EVGA 850w Platinum | Display Asus PB258Q 1440p

| Cooling Noctua NH D15 | Keyboard Cooler master mechanical kb with blue switches | Mouse Logitech G502 | Sound Beyerdynamic DT 990

| Laptop Maingear Vector Pro 2 (Ryzen 9 6900xh, Nvidia 3070t, 32gb ram)

 

 

Link to post
Share on other sites

You're not calling the input function and you never return anything.

oh crap, yeah I forgot to make the function return something, thanks man

CP: Ryzen 7 2700x | Motherboard AsRock B450M | RAM 32GB Corsair ddr4 | GPU EVGA (RIP) 1070ti | Case Corsair Vengeance C-70 Black | Storage 512gb Samsung NVME ssd | PSU EVGA 850w Platinum | Display Asus PB258Q 1440p

| Cooling Noctua NH D15 | Keyboard Cooler master mechanical kb with blue switches | Mouse Logitech G502 | Sound Beyerdynamic DT 990

| Laptop Maingear Vector Pro 2 (Ryzen 9 6900xh, Nvidia 3070t, 32gb ram)

 

 

Link to post
Share on other sites

You're not calling the input function and you never return anything.

but it's still giving an error for some reason, it has something to do with the line:

 row = GetRowInput("Please enter an integer: ")

but I'm not sure what, I think it is just bypassing the first try part of the function for some reason which then causes nothing to work

CP: Ryzen 7 2700x | Motherboard AsRock B450M | RAM 32GB Corsair ddr4 | GPU EVGA (RIP) 1070ti | Case Corsair Vengeance C-70 Black | Storage 512gb Samsung NVME ssd | PSU EVGA 850w Platinum | Display Asus PB258Q 1440p

| Cooling Noctua NH D15 | Keyboard Cooler master mechanical kb with blue switches | Mouse Logitech G502 | Sound Beyerdynamic DT 990

| Laptop Maingear Vector Pro 2 (Ryzen 9 6900xh, Nvidia 3070t, 32gb ram)

 

 

Link to post
Share on other sites

but it's still giving an error for some reason, it has something to do with the line:

 row = GetRowInput("Please enter an integer: ")

but I'm not sure what, I think it is just bypassing the first try part of the function for some reason which then causes nothing to work

Because you're still not calling input(), look at the code SSL gave you compared to yours.

1474412270.2748842

Link to post
Share on other sites

Because you're still not calling input(), look at the code SSL gave you compared to yours.

Man I'm dumb lol, sorry for the trouble I'm a bit out of touch with programming so I'm trying to get back into it. Thanks for all the help, it works like a charm now!

CP: Ryzen 7 2700x | Motherboard AsRock B450M | RAM 32GB Corsair ddr4 | GPU EVGA (RIP) 1070ti | Case Corsair Vengeance C-70 Black | Storage 512gb Samsung NVME ssd | PSU EVGA 850w Platinum | Display Asus PB258Q 1440p

| Cooling Noctua NH D15 | Keyboard Cooler master mechanical kb with blue switches | Mouse Logitech G502 | Sound Beyerdynamic DT 990

| Laptop Maingear Vector Pro 2 (Ryzen 9 6900xh, Nvidia 3070t, 32gb ram)

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×