[Tutor] python help!
D.V.N.Sarma డి.వి.ఎన్.శర్మ
dvnsarma at gmail.com
Sun Feb 10 02:36:14 CET 2013
I have altered the program to run on Python 2.7.3
Perhaps it will run on Python 3 with small/or no alterations.
def show_menu():
print("=======================")
print("1-binary to denary")
print("2-denary to binary")
print("3-exit")
print("=======================")
while True:
show_menu()
choice = raw_input("please enter an option: ")
if choice == '1':
binary = raw_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)
elif choice == '2':
denary2 = int(raw_input("Please enter a denary number: "))
remainder = ''
if denary2 not in range(0,256):
denary2 = int(input("Please enter a denary number: "))
continue
while denary2 > 0:
remainder = str(denary2 % 2) + remainder
denary2 /= 2
print("The result is",remainder)
elif choice == '3': break
elif choice == '1' or choice == '2' or choice == '3':
print("Invalid input-try again!")
On Sun, Feb 10, 2013 at 12:50 AM, Peter Otten <__peter__ at web.de> wrote:
> Ghadir Ghasemi wrote:
>
> > Hi guys can you tell me what is wrong with the second part of this
> > code(elif choice == '2'). When I type something other than 0-255, it
> > correctly asks again for an input but when I enter a number from 0-255 it
> > does nothing :
>
> It doesn't do nothing, it keeps running the while loop. Add a print() call
>
> > elif choice == '2':
> > denary2 = int(input("Please enter a denary number: "))
> > remainder = ''
> > while denary2 not in range(0,256):
> > denary2 = int(input("Please enter a denary number: "))
> > continue
> > while denary2 in range(0,256):
>
> print("XXX denary2 =", denary2)
>
> > remainder = str(denary2 % 2) + remainder
> > denary2 >>= 1
> > print("The result is",remainder)
>
> to see what's happening.
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
regards,
Sarma.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130210/a2df11fc/attachment.html>
More information about the Tutor
mailing list