• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

is this clumsy?  RSS feed

 
Ranch Foreman
Posts: 134
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Particularly this line:
while userChoice != 1 and userChoice !=2 and userChoice !=3 and userChoice !=4 and userChoice !=5 and userChoice != 6 :

The reason im asking is because my teacher mentioned this to get the loop going for the menu
but im sure there is a far more elegant way of doing this
simple sentinel variable for when you got an answer? is that what you would do?

 
wayne brandon
Ranch Foreman
Posts: 134
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is better

 
Greenhorn
Posts: 26
Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wayne,

Yes the "if/elif/else" construct is often used to solve problems like this .  Python does not have a "case..end case" construct in the language so the code shown in the second example is the usual way to address this pythonically.  There are more sophisticated ways to let the data drive the selection, but may I suggest that you let that be a learning experience much further in your Python journey?  🙂
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!