#πŸ”’ Beginner Assignment (Number Systems Conversion)

1129 messages Β· Page 2 of 2 (latest)

fallen pier
#

oh its in the if

drowsy jay
#

yes

fallen pier
#

alr

drowsy jay
#

correct?

fallen pier
#

yes

drowsy jay
#

so what do we do?

fallen pier
#

for the binary we have to add?

drowsy jay
#

yes, so Closet = Closet + splits

fallen pier
#

not Closet = Closet + "." + splits?

drowsy jay
#

no

#

we have to add the first integer everytime

#

but the dot only once before we add any decimal part

fallen pier
#

ah cause its in the while loop

drowsy jay
#

yes

fallen pier
#

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
drowsy jay
#

yes!!

fallen pier
#
        while decimal < 1:
          decimal * 2
          splits = str(decimal)[0]
          Closet = Closet + splits
          print("Result:",Closet)

its complete?

drowsy jay
#

decimal does not change here

fallen pier
#

where

drowsy jay
#

decimal * 2

#

you don't store it

fallen pier
#

decimal = decimal * 2?

drowsy jay
#

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?

fallen pier
#

so we can determine if it is 0 or 1 for the decimal part?

drowsy jay
#

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

fallen pier
#

oh

drowsy jay
#

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

fallen pier
#

we need to do decimal = decimal % 1 so that it counts the remainder?

drowsy jay
#

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

fallen pier
#

hmmm like this?

        while decimal < 1:
          decimal = decimal * 2 
          splits = str(decimal)[0]
          Closet = Closet + splits
          decimal = decimal % 1
        print("Result:",Closet)
drowsy jay
#

exactly

fallen pier
#

i think we have to turn it into a float?

drowsy jay
#

can you show that in your code?

fallen pier
#

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:
?

drowsy jay
#

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

fallen pier
#

hmmm I see

#

DeciBin is the full number
integer is the whole number we separated

drowsy jay
#

yes

fallen pier
#

so like integer = int(integer)?

drowsy jay
#

it already is an int

#

look closely

fallen pier
#
        integer = int(splitting[0])
#

this part

drowsy jay
#

yes

fallen pier
drowsy jay
#

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

fallen pier
#

i see it now

drowsy jay
#
      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

fallen pier
#
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

drowsy jay
#

we can clean it up a bit though

fallen pier
#

i kinda like how it is, tho it is hard to read

drowsy jay
#

be careful

drowsy jay
fallen pier
#

oh

drowsy jay
#

have you seen functions?

fallen pier
#

functions?

drowsy jay
#

yes

fallen pier
#

in the document?

drowsy jay
#

I will explain

#

but basically we can make the code cleaner to read

#

first, does it work?

fallen pier
#

not really

drowsy jay
#

Oh I know why

#
      while decimal < 1:
          decimal = decimal * 2 
          splits = str(decimal)[0]
          Closet = Closet + splits
          decimal = decimal % 1
fallen pier
#

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)

drowsy jay
#

we convert decimal to a value below 1 always

fallen pier
#

hmmm

drowsy jay
#

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

fallen pier
#

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)
drowsy jay
#

do you want to learn functions also or good for now?

fallen pier
#

but

#

one more thing

drowsy jay
#

yea

fallen pier
#

I have to limit the result to 4 decimal places

bleak elk
#

Calculate 5 then truncate

fallen pier
#

so lets say 75.95 limit to 1001011.() () () ()

fallen pier
drowsy jay
#

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

fallen pier
#

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

drowsy jay
#

add count in the loop

fallen pier
#

the count = 0 or the count += 1?

drowsy jay
#

+1

#

i have to go now, you can ping me later and i can explain functions

fallen pier
drowsy jay
#

oh ok

fallen pier
#

i'll try to make the rest on my own, if I can...

drowsy jay
#

gn !!

fallen pier
#

!close

celest wedgeBOT
#
Python help channel closed with !close

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.