Posts: 2
Threads: 1
Joined: Nov 2017
Nov-05-2017, 12:42 PM
(This post was last modified: Nov-05-2017, 12:42 PM by t0mhun13r.)
I have made a program that asks if you want to convert from denary to binary, or binary to denary. It is to help me practice binary. There are two options, 1 and 2. Input 1 tells you binary to convert to denary. Input 2 is supposed to tell you a denary and you need to convert it to binary.
The input 1 works, but I can't find out why the input 2 (Denary to binary) won't work.
How would I make this program work?
import random
binaryTotal = 0
done = False
choice = input("Would you like to try:\n1. Converting from binary to denary\n2. Converting from denary to binary")
binaryRand1 = str(random.randint(0,1))
binaryRand2 = str(random.randint(0,1))
binaryRand3 = str(random.randint(0,1))
binaryRand4 = str(random.randint(0,1))
binaryRand5 = str(random.randint(0,1))
binaryRand6 = str(random.randint(0,1))
binaryRand7 = str(random.randint(0,1))
binaryRand8 = str(random.randint(0,1))
binary = (binaryRand1)+(binaryRand2)+(binaryRand3)+(binaryRand4)+(binaryRand5)+(binaryRand6)+(binaryRand7)+(binaryRand8)
if binary[0]=="1":
binaryTotal += 128
if binary[1]=="1":
binaryTotal += 64
if binary[2]=="1":
binaryTotal += 32
if binary[3]=="1":
binaryTotal += 16
if binary[4]=="1":
binaryTotal += 8
if binary[5]=="1":
binaryTotal += 4
if binary[6]=="1":
binaryTotal += 2
if binary[7]=="1":
binaryTotal += 1
while done == False:
if choice == 1:
answer = input("What is "+binary+" in denary?")
if answer == binaryTotal:
print("Well done, you got it!")
done = True
else:
print("Try again, remember binary goes: 128, 64, 32, 16, 8, 4, 2, 1\n")
elif choice == 2:
answer = input("What is "+ str(binaryTotal) +" in binary?")
if answer == binary:
print("Well done, you got it!")
done = True
elif answer != binary:
print("Try again, remember binary goes: 128, 64, 32, 16, 8, 4, 2, 1\n")
Posts: 1,298
Threads: 38
Joined: Sep 2016
Nov-05-2017, 04:11 PM
(This post was last modified: Nov-05-2017, 04:12 PM by sparkz_alot.)
So you want to be able to convert from binary (base 2) to decimal (base 10) and vice versa, correct? Are you restricted by what library's you can import?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 20
Threads: 2
Joined: Nov 2017
Nov-05-2017, 04:58 PM
(This post was last modified: Nov-05-2017, 04:58 PM by ineedastupidusername.)
after line 46 type this (I often put "hi" or something dumb so the location of that test print is obvious)
print 'hi', isinstance(answer,basestring),isinstance(binary,basestring)
So the problem was answer wasn't a string, so if str(answer)==binary should fix it (didn't test yet)
Here's a shorter way to make a binary list and decimal total, if interested, but yours works fine:
binarylist=[]
powersof2=[128,64,32,16,8,4,2,1]
total=0
for i in range(8):
bit=random.choice(('0','1'))
binarylist.append(bit)
if bit=='1':
total+=powersof2[i]
print ''.join(binarylist)
print total
Posts: 2
Threads: 1
Joined: Nov 2017
Ok so now I have this. It works if the binary number starts with a 1 (eg. 10100100), but not if it starts with a 0 (eg. 01001011).
How do I fix this?
import random
binaryTotal = 00000000
done = False
choice = input("Would you like to try:\n1. Converting from binary to denary\n2. Converting from denary to binary")
binaryRand1 = str(random.randint(0,1))
binaryRand2 = str(random.randint(0,1))
binaryRand3 = str(random.randint(0,1))
binaryRand4 = str(random.randint(0,1))
binaryRand5 = str(random.randint(0,1))
binaryRand6 = str(random.randint(0,1))
binaryRand7 = str(random.randint(0,1))
binaryRand8 = str(random.randint(0,1))
binary = (binaryRand1)+(binaryRand2)+(binaryRand3)+(binaryRand4)+(binaryRand5)+(binaryRand6)+(binaryRand7)+(binaryRand8)
if binary[0]=="1":
binaryTotal += 128
if binary[1]=="1":
binaryTotal += 64
if binary[2]=="1":
binaryTotal += 32
if binary[3]=="1":
binaryTotal += 16
if binary[4]=="1":
binaryTotal += 8
if binary[5]=="1":
binaryTotal += 4
if binary[6]=="1":
binaryTotal += 2
if binary[7]=="1":
binaryTotal += 1
while done == False:
if choice == 1:
answer = input("What is "+binary+" in denary?")
if answer == binaryTotal:
print("Well done, you got it!")
done = True
else:
print("Try again, remember binary goes: 128, 64, 32, 16, 8, 4, 2, 1\n")
elif choice == 2:
answer = input("What is "+ str(binaryTotal) +" in binary?")
if str(answer) == binary:
print("Well done, you got it!")
done = True
elif str(answer) != binary:
print("Try again, remember binary goes: 128, 64, 32, 16, 8, 4, 2, 1\n")
Posts: 20
Threads: 2
Joined: Nov 2017
lol, apparently input(00011011) with a leading zero makes it think it is in octal base. So... I guess you're supposed to use raw_input() instead of input(). Don't ask me, I just search engined that answer.
Posts: 1,298
Threads: 38
Joined: Sep 2016
Hmm, it shouldn't work at all.
This line is asking for a string:
choice = input("Would you like to try:\n1. Converting from binary to denary\n2. Converting from denary to binary") yet in your "while" statement you are comparing "choice" to an integer.
Quote:I guess you're supposed to use raw_input() instead of input()
raw_input() is used in Python version 2.x, while input() is used in version 3.x
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 20
Threads: 2
Joined: Nov 2017
In python 2.7 I was able to use both, but this is the behavior I got:
rawinp= raw_input("type 00011011\n")
inp=input("type 00011011\n")
print 'raw_input keeps starting 0s', str(rawinp) #outputs 00011011
print 'input thinks start 0s means octal', str(inp) #outputs 4617 so raw_input gave the desired output with starting 0s whereas input gave 4617 because it interpreted something with starting zeros as octal I think
Posts: 1,298
Threads: 38
Joined: Sep 2016
You can use both in Python 2.x, however, there is a difference.
From the Docs:
Quote:input([prompt])
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.
This "evaluation" can result in some strange results, if not errors, if not used correctly (as you can see from your examples).
As a side note, Octal 4617 would be 1001 1000 1111 in binary.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
|