[Tutor] Loop Exception Handles

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Mar 16 05:36:24 CET 2013


On 16/03/2013 04:08, Vincent Balmori wrote:
> I am trying to loop a simple exception. I tried to put a while loop, but
> I keep getting syntax issues. I also tried to alternatively use
> something on the lines of "while number != int" also with no avail.

Please help us to help you.  What did your while loop look like and what 
were the syntax issues?  Tell us precisely what your alternative was and 
what went wrong, syntax error, value error, power cut, atom bomb dropped?

>
> def main():
>      print("\t\tWelcome to Blackjack!\n")
>      names = []
>      try:
>          number = games.ask_number("How many players? (1 - 7): ", low =
> 1, high = 8)
>      except (ValueError, TypeError):
>          print("That is not a number!")
>          number = games.ask_number("How many players? (1 - 7): ", low =
> 1, high = 8)
>          continue

This must be a syntax error as continue is only valid in a loop of some 
kind.  Try (awful I know) something like

number = 0
while not number:
     try:
         number = games.ask_number("How many players? (1 - 7): ", low = 
  1, high = 8)
     except (ValueError, TypeError):
         print("That is not a number!")

>      for i in range(number):
>          name = input("Enter player name: ")
>          names.append(name)
>      print()
>
>

Assuming that you get your while loop to work correctly, how many times 
will the for loop above run if you enter 1?

-- 
Cheers.

Mark Lawrence



More information about the Tutor mailing list