On 10 February 2013 15:29, Ghadir Ghasemi <[email protected]> wrote:
> Hi guys, I wondered if you knew what I could add to this code so that when
> the user enters 1 from the menu and then doesn't enter a valid binary number
> the program should ask them over and over again until valid binary number is
> entered.
> here is the code:
<snip>
> while True:
> show_menu()
>
> choice = input("please enter an option: ")
>
> if choice == '1':
> binary = input("Please enter a binary number: ")
> denary = 0
> place_value = 1
>
> for i in binary [::-1]:
> denary += place_value * int(i)
> place_value *= 2
>
> print("The result is",denary)
<snip>
> elif choice == '3':
> break
Hi Ghadir,
"over and over again until" suggests a while loop.
So, you need something like the pseudo-code:
while True:
binary = input("Please enter a binary number: ")
if isgood(binary):
pass # do stuff then break
else:
pass #remind user of constraints before they are prompted again
(This assumes you've an isgood function that will return a boolean as
the user input is acceptable. That's not necessarily the best way, but
it makes for easy pseduo-code and doesn't do the work for you ;-)
Last, a better subject line is a good idea. Pretty much every post
asking for help from someone who's posted before could have your
subject line.
Best,
Brian vdB
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor