Help needed for a first-year computer science exercise
SalaNsi Posted messages 13 Status Member -
7 answers
-
Hello,
Rather than an image of the code, we prefer it to be in the message, with the source icon and the Python language which gives:
for i in range(4): print(i)
Your statement indicates that the power of 2 is a positional calculation from right to left, so you cannot simply take i. After that, you need to extract the position of the binary with a binary[i] syntax.
-
I understand the code in the message, but after that I don't see how to do it from right to left, and even less how to extract the position of the binary since we haven't covered it in the course.
-
Depending on the value of i, from 0 to 3, you will extract the bit to be processed with binaire[i]
Next, for the power of 2, you need to find a formula that combines i (varying from 0 to 3) and len(binaire) which equals 4 where
- when i=0 power of 2 = 3
- when i=1 power of 2 = 2
- when i=2 power of 2 = 1
- when i=3 power of 2 = 0
-
-
I'm sorry, but even after searching for a long time, I still haven't found what needs to be put.
-
Thank you for that, but honestly I'm not lying to you, I have 3 other assignments in the homework and I don't know what the teacher has in mind, we don't have the course, he just gave us some tools to supposedly help us.
-
I even started the 2nd exercise but I am still having difficulties
here is the code for the 2nd and the instructions linked

:
def dec_to_bin(decimal):
binary=""
quotient = decimal // 2
remainder = decimal % 2
binary = remainder + binary
while quotient!=0 :
decimal = quotient
remainder = decimal % 2
binary = remainder + binary
quotient = decimal//2
# in the end, as long as the binary does not have a size of 4
while len(binary) != 4:
# we add "0"s to the left
binary = ...
return binary
# The lines below test your code... don't touch them!
# If an AssertionError appears... it means your code is not correct...
assert dec_to_bin(10) == '1010'
assert dec_to_bin(15) == '1111'
assert dec_to_bin(0) == '0000' -
I didn't check if this site offers a Python course.
Search for the site "Zeste De Savoir" the course "Zeste de Python" written by "entwanne". -
but actually I don't have much time because the assignment is due on Monday.
