Jump to content

Need Python failsafe help

tengarp
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?

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
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 comment
Share on other sites

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
Link to comment
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.

 

 

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!

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

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: ")
CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

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

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

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

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

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 comment
Share on other sites

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!

CPU i5 3570k @ 4.5GHz Motherboard MSI Z77A-G43 RAM 16GB Patriot Somthingortheother GPU XFX 7870 GHz Edition Case Corsair Vengeance C-70 Black Storage 120GB Kingston HyperX 3k + 1tb Seagate HDD PSU Corsair HX650  

Displays 2 old 1080p Acer and Samsung ones Cooling Corsair H60

† TTCF Member †

Link to comment
Share on other sites

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

×