Re: [Tutor] Creating an Invalid Message for user
- Original Message - > From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Saturday, April 19, 2014 3:58 AM > Subject: Re: [Tutor] Creating an Invalid Message for user > > Hello Saba, and welcome, > > Saba, unfortunately your email is almost unreadable to me. To me, your > code looks like this: > > On Fri, Apr 18, 2014 at 10:19:52PM +0100, Saba Usmani wrote: > >> print "Welcome to the binary and decimal converter"loop = > Truewhile >> loop: bord = raw_input("Enter b for binary or d decimal or exit to >> exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input > ("Enter >> Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): > if((int(b) % >> 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal >> Number is: ", d elif bord == "d": x=0 n=int(input('Enter > Decimal >> Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) >> k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: >> string=string+str(j) print('The binary Number for %d is %s'%(x, >> string)) elif bord == "exit" : print "Goodbye" loop = > False > > A complete mess! Unfortunately, I have neither the time nor the > inclination to spend a lot of effort trying to unmangle the code to see > what you intended it to be. Hmmm, this indeed looks like Perl. Or worse yet: Brainf*ck, http://www.hevanet.com/cristofd/brainfuck/dbf2c.b ;-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating an Invalid Message for user
Hello Saba, and welcome, Saba, unfortunately your email is almost unreadable to me. To me, your code looks like this: On Fri, Apr 18, 2014 at 10:19:52PM +0100, Saba Usmani wrote: > print "Welcome to the binary and decimal converter"loop = Truewhile > loop: bord = raw_input("Enter b for binary or d decimal or exit to > exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input ("Enter > Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): if((int(b) % > 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal > Number is: ", d elif bord == "d": x=0 n=int(input('Enter Decimal > Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) > k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: > string=string+str(j) print('The binary Number for %d is %s'%(x, > string)) elif bord == "exit" : print "Goodbye" loop = False A complete mess! Unfortunately, I have neither the time nor the inclination to spend a lot of effort trying to unmangle the code to see what you intended it to be. As a programmer, you will often be dealing with text formats, and with text it is very important that your email program (Outlook, it seems) doesn't mess up the layout. Especially with Python. Unfortunately, if your email program is configured to send so-called "Rich Text" (actually HTML, exactly the same format that web pages use) a side-effect is that it may mess up the layout as above. I recommend that, when posting to technical forums like this tutor mailing list, you turn off "Rich Text" posting so we can see the code the way it is meant to be seen. If you help us to see your code the way it should be seen, we can help you with your code. Regards, -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating an Invalid Message for user
On 18/04/14 22:19, Saba Usmani wrote: I am meant to design code for a program that converts from binary number to decimal and vice versa. First, you know that python includes functions for doing that already right? So you are just doing this as a learning exercise? while loop: bord = raw_input("Enter b for binary or d decimal or exit to exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input ("Enter Binary Number:") b=b.lstrip("0") Before converting to an int check each character is in the numeric range you need. For binary thats like for ch in inputstring: if ch not in "01": # process invalid input For decimal its for ch in inputstring: if ch not in "0123456789": # process bad input. You could put that in a function and pass in the numbers as a parameter... If you accept floats as input then it gets a whole lot more complicated. but you are converting to int so I assume its ints you expect. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Creating an Invalid Message for user
Hi, I am meant to design code for a program that converts from binary number to decimal and vice versa. This is what i have so far: print "Welcome to the binary and decimal converter"loop = Truewhile loop: bord = raw_input("Enter b for binary or d decimal or exit to exit")if bord == "b":d = 0b = 0factor = 1;b = raw_input ("Enter Binary Number:")b=b.lstrip("0")b = int(b) while(b > 0):if((int(b) % 10) == 1):d += factor b /= 10factor = factor * 2print "The Decimal Number is: ", d elif bord == "d":x=0n=int(input('Enter Decimal Number: '))x=nk=[] # arraywhile (n>0): a=int(float(n%2))k.append(a)n=(n-a)/2 k.append(0)string=""for j in k[::-1]: string=string+str(j)print('The binary Number for %d is %s'%(x, string)) elif bord == "exit" :print "Goodbye"loop = False - This code does not recognize invalid inputs e.g in the binary to decimal conversion, so if I enter e.g 10021, not a binary number, it will not inform me,the user, that the input is invalid. The same problem occurs with the decimal to binary conversion - if i enter e.g 123&&gf I am not told to try again with a valid input - how do I implement this in the code above ThanksSaba___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating an Invalid message for user
Saba Usmani Wrote in message: > > You posted in html so I can't quote your code, but why aren't you using int() to convert in one call? Second argument is the base to be used. value = int ("10011", 2) othervalue = int ("234") # default to decimal -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating an Invalid message for user
On 11/04/2014 21:58, Saba Usmani wrote: Hi, I am meant to design code for a program that converts from binary number to decimal and vice versa. This is what i have so far: print "Welcome to the binary and decimal converter" loop = True while loop: bord = raw_input("Enter b for binary or d decimal or exit to exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input ("Enter Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): if((int(b) % 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal Number is: ", d elif bord == "d": x=0 n=int(input('Enter Decimal Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: string=string+str(j) print('The binary Number for %d is %s'%(x, string)) elif bord == "exit" : print "Goodbye" loop = False - This code does not recognize invalid inputs e.g in the binary to decimal conversion, if I enter 10021 it will not inform me,the user, that the input is invalid. The same problem occurs with the decimal to binary conversion - if i enter 123&&gf I am not told to try again with a valid input - how do I implement this in the code above Thanks Saba https://docs.python.org/3/tutorial/errors.html#handling-exceptions is as good a starting point as any. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Creating an Invalid message for user
Hi, I am meant to design code for a program that converts from binary number to decimal and vice versa. This is what i have so far: print "Welcome to the binary and decimal converter"loop = Truewhile loop: bord = raw_input("Enter b for binary or d decimal or exit to exit")if bord == "b":d = 0b = 0factor = 1;b = raw_input ("Enter Binary Number:")b=b.lstrip("0")b = int(b) while(b > 0):if((int(b) % 10) == 1):d += factor b /= 10factor = factor * 2print "The Decimal Number is: ", d elif bord == "d":x=0n=int(input('Enter Decimal Number: '))x=nk=[] # arraywhile (n>0): a=int(float(n%2))k.append(a)n=(n-a)/2 k.append(0)string=""for j in k[::-1]: string=string+str(j)print('The binary Number for %d is %s'%(x, string)) elif bord == "exit" :print "Goodbye"loop = False - This code does not recognize invalid inputs e.g in the binary to decimal conversion, if I enter 10021 it will not inform me,the user, that the input is invalid. The same problem occurs with the decimal to binary conversion - if i enter 123&&gf I am not told to try again with a valid input - how do I implement this in the code above ThanksSaba___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor