• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

try except loop isnt working  RSS feed

 
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does try except need to be in a loop?
its not stopping text being entered...its crashing

 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it sorted!!!

 
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
Which language is that?
You will find the loop easier to read if you change the name of the flag variable, so the loop runs when the flag variable is true.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its Python Campbell (2 languages for $500)

"change the name of the flag variable, so the loop runs when the flag variable is true."

so

While selecting True
     selected = False

to me it made sense more the other way round

While selecting False
     selected = True

do you understand what i mean?
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i obviously want to go along with best practices

and i guess your easoning is

while this block is true run it...till its made false

 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . (2 languages for $500) . . .

Get another two languages and you can fit the price per language into a byte


While selecting True
     selected = False

to me it made sense more the other way round

While selecting False
     selected = True

do you understand what i mean?

You need to change the name of the variable; don't write selecting and selected.Read the code out and see which is easier to understand.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about that
i chose python in the new post menu but it still put it in java

funny byte joke by the way!!!
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My jokes are never funny
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i rewrote the loop like this and it seems to work way more consistently

qqiSelected = False
moduleCount = 0
while not qqiSelected:
   try:
       qqiLevel = int(input("Please enter QQI level number (5 or 6): "))

       if qqiLevel != 5 and qqiLevel != 6:
           print("Invalid input...")
       else:
           moduleCount = qqiLevel
           qqiSelected = True

   except ValueError:
       print("Invalid input...")

print(moduleCount)
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my tutor said this

The only statement that should be in the try is the line where you take input and try to convert to an int.
This is the line that could crash the program if a non-int is entered.
You can check if it’s a valid integer (5 or 6) after the try – catch but still within the loop.

--i dont really understand this,i tried moving things around but just got errors
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . The only statement that should be in the try is the line where you take input and try to convert to an int.  . . .

It does no good trying moving things around and seeing whether they work. That way lies confusion.

I have a concept about exception handling: what are you going to do if an exception is thrown? It is probably more complicated in Java®, which “forces” users to handle certain kinds of exception. What are you going to do if the input in line 5 isn't an int? Are you going to set the input to 0 and let the rest of the loop run? In that case it would be reasonable to wrap line 5 alone in the try. What if you decide you are going to miss out the remainder of the loop body, print an error message, and start the loop again? Then it would be better to include the whole of the loop in the try. It is a case of where you intend control to pass if an exception is thrown. Yer pays yer money and yer takes yer choice.
So I don't like a bald statement that only the one line causing the exception should be included in the try.

I am sure other people will have other opinions.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!