#π Beginner Assignment (Number Systems Conversion)
1129 messages Β· Page 2 of 2 (latest)
yes
alr
correct?
yes
so what do we do?
for the binary we have to add?
yes, so Closet = Closet + splits
not Closet = Closet + "." + splits?
no
we have to add the first integer everytime
but the dot only once before we add any decimal part
ah cause its in the while loop
yes
so this
if len(Closet) == 0:
Closet = "0"
Closet = Closet[::-1]
Closet = Closet + "."
while decimal < 1:
decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
yes!!
while decimal < 1:
decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
print("Result:",Closet)
its complete?
decimal does not change here
where
decimal = decimal * 2?
yes
and also
we then have to do the steps again with the new decimal part
so we need to do decimal = decimal % 1 at the end
do you see why?
so we can determine if it is 0 or 1 for the decimal part?
no, we have already done that
while decimal < 1:
decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
print("Result:",Closet)
this does that already
oh
but now decimal will be 1.50 the first time after doing * 2
and then stay that way
but the next time you do the loop you need 0.50
we need to do decimal = decimal % 1 so that it counts the remainder?
yes
and then at the end you can do print("Result:",Closet)
but outside the loop again
so only prints after all the conversion
and then test with 75.25
hmmm like this?
while decimal < 1:
decimal = decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
decimal = decimal % 1
print("Result:",Closet)
exactly
can you show that in your code?
this part?
elif len(splitting) == 2: #decimal number
splitting = str(DeciBin).split(".")
integer = int(splitting[0])
decimal = float(DeciBin) - integer
Closet = ""
while DeciBin > 0:
Remaining = DeciBin % 2
Closet = Closet + str(Remaining)
DeciBin = DeciBin // 2
if len(Closet) == 0:
Closet = "0"
Closet = Closet[::-1]
Closet = Closet + "."
while decimal < 1:
decimal = decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
decimal = decimal % 1
print("Result:",Closet)
after the elif len(splitting) == 2:
?
yes, so we can use Γ¬nteger there
because in that case DeciBin is the full decimal number
an that's not what we want to use
yes
so like integer = int(integer)?
yes
hmmm so why is it showing this error message
because you are using Decibin
elif len(splitting) == 2: #decimal number
splitting = str(DeciBin).split(".")
integer = int(splitting[0])
decimal = float(DeciBin) - integer
Closet = ""
while DeciBin > 0:
Remaining = DeciBin % 2
Closet = Closet + str(Remaining)
DeciBin = DeciBin // 2
here
you should replace that with integer
i see it now
elif len(splitting) == 2: #decimal number
splitting = str(DeciBin).split(".")
integer = int(splitting[0])
decimal = float(DeciBin) - integer
Closet = ""
while integer > 0:
Remaining = integer % 2
Closet = Closet + str(Remaining)
integer = integer // 2
like so
if choose == "A":
DeciBin = (input("Enter a Decimal: "))
print("Decimal to Binary Conversion")
splitting = str(DeciBin).split(".")
if len(splitting) == 1: #integer
DeciBin = int(DeciBin)
Closet = ""
while DeciBin > 0:
Remaining = DeciBin % 2
Closet = Closet + str(Remaining)
DeciBin = DeciBin // 2
if len(Closet) == 0:
Closet = "0"
Closet = Closet[::-1]
print("Result:", Closet)
elif len(splitting) == 2: #decimal number
splitting = str(DeciBin).split(".")
integer = int(splitting[0])
decimal = float(DeciBin) - integer
Closet = ""
while integer > 0:
Remaining = integer % 2
Closet = Closet + str(Remaining)
DeciBin = integer // 2
if len(Closet) == 0:
Closet = "0"
Closet = Closet[::-1]
Closet = Closet + "."
while decimal < 1:
decimal = decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
decimal = decimal % 1
print("Result: ", Closet)
man
that is so long
i kinda like how it is, tho it is hard to read
be careful
well we can clean that up
oh
have you seen functions?
functions?
yes
in the document?
I will explain
but basically we can make the code cleaner to read
first, does it work?
not really
Oh I know why
while decimal < 1:
decimal = decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
decimal = decimal % 1
i enjoy the process of programming but not when they give me this type of assignment on my 3rd week π
(yes it was given last week, but the deadline is extended)
we convert decimal to a value below 1 always
hmmm
while decimal < 1: this will only check when
decimal = decimal % 1 this is done
but that will always result in a value less than one
so like while decimal <= 1:?
oh
we make it to
decimal = decimal % 2?
it works π
print("-----------------------------------------------------")
print("Welcome to Number Systems Conversion")
print("-----------------------------------------------------")
print(" ")
print("Please choose the letter below regarding your conversion process.")
print("A - Decimal to Binary")
print("B - Decimal to Octal")
print("C - Decimal to Hexadecimal")
print("D - to Decimal")
print("E - to Decimal")
print("F - to Decimal")
print(" ")
choose = input("Enter: ").strip().upper()
if choose == "A":
DeciBin = (input("Enter a Decimal: "))
print("Decimal to Binary Conversion")
splitting = str(DeciBin).split(".")
if len(splitting) == 1: #integer
DeciBin = int(DeciBin)
Closet = ""
while DeciBin > 0:
Remaining = DeciBin % 2
Closet = Closet + str(Remaining)
DeciBin = DeciBin // 2
if len(Closet) == 0:
Closet = "0"
Closet = Closet[::-1]
print("Result:", Closet)
elif len(splitting) == 2: #decimal number
splitting = str(DeciBin).split(".")
integer = int(splitting[0])
decimal = float(DeciBin) - integer
Closet = ""
while integer > 0:
Remaining = integer % 2
Closet = Closet + str(Remaining)
integer = integer // 2
if len(Closet) == 0:
Closet = "0"
Closet = Closet[::-1]
Closet = Closet + "."
while decimal < 1:
decimal = decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
decimal = decimal % 2
print("Result: ", Closet)
great !!!
do you want to learn functions also or good for now?
sure
but
one more thing
yea
I have to limit the result to 4 decimal places
Calculate 5 then truncate
so lets say 75.95 limit to 1001011.() () () ()
truncate?
you can add a count = 0 above the while loop
and do if count > 4: break
break will also stop the while loop
and then in the loop, you can do count = count + 1
hmmm
which while loop
theres 2
i had a classmate who did Precision
count = 0
while decimal < 1:
decimal = decimal * 2
splits = str(decimal)[0]
Closet = Closet + splits
decimal = decimal % 2
if count > 4:
break
print("Result: ", Closet)
count += 1
hmmm now it only shows 1 decimal place, but its 1 and not 0
add count in the loop
the count = 0 or the count += 1?
Alright, thanks a lot!
It's currently 12:29am for me though
oh ok
i'll try to make the rest on my own, if I can...
gn !!
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.