简体   繁体   English

使用python中的函数将十进制数字转换为二进制

[英]Converting decimal numbers to binary with functions in python

I have to create a program to convert decimal numbers to binary for my class but im lost. 我必须为我的班级创建一个将十进制数字转换为二进制的程序,但即时消息丢失了。 These are the directions. 这些是方向。 3 Files must be used 必须使用3个文件

user_input.py user_input.py

Obtain an integer from the user between 0-255. 向用户获取一个介于0到255之间的整数。
No conversion or print statements allowed in the user_input.py user_input.py中不允许任何转换或打印语句
Obtaining the integer must be done in a function 获取整数必须在函数中完成
No import statements allowed 不允许导入声明

conversion.py conversion.py

Convert the number provided to a binary number (eg, 254 provided and converted to 11111110) 将提供的数字转换为二进制数(例如,提供254并转换为11111110)
No print statements, input statements, or built in functions to convert a number to binary. 没有用于将数字转换为二进制的打印语句,输入语句或内置函数。 Conversion must be done manually. 转换必须手动完成。
Small hint, concatenating the 1s and 0s is going to be tough b/c python will want to add the numbers (eg, 1+1 = 2 vs 1+1 = 11). 小提示,将1和0串联起来将很困难b / c python将要添加数字(例如1 + 1 = 2 vs 1 + 1 = 11)。 So, how are you going to handle this issue? 那么,您将如何处理此问题?
The conversion must be done in a function 转换必须在函数中完成
No import statements allowed 不允许导入声明

main.py main.py

Print the eight bit binary representation (00000000 - 11111111) of the number provided and converted. 打印提供并转换的数字的八位二进制表示形式(00000000-11111111)。 To do so, you will have to access the necessary functions in conversion.py and user_input.py 为此,您将必须访问conversion.py和user_input.py中的必要功能
Note - the program is only allowed to access functions. 注意-该程序仅允许访问功能。 No direct access to variables allowed. 不允许直接访问变量。
No input or conversions allowed in main.py Note - the binary print must only be the eight binary numbers. main.py中不允许输入或转换。注意-二进制打印只能是八个二进制数字。 No commas, brackets etc. - ['1', '0', '0', ...] or [1,1,0,0,1..] are not allowed. 禁止使用逗号,方括号等。-不允许使用['1','0','0',...]或[1,1,0,0,1 ..]。

to make the function to get the inputted number I use 使函数获取我使用的输入号码

  def number ():
    num =int(input('Pick a number from 0 - 255'))
    return num
  number()

After that when I do not know how I would access it without importing it. 之后,当我不知道如何导入而不导入它时。 This is the code I'm using to convert 这是我用来转换的代码

def conversion(num):
cnv_bin = ''
num = ''
while num // 2 > 0:
    if num % 2 == 0:
        cnv_bin += '0'
    else:
        cnv_bin += '1'
    num = num // 2
return cnv_bin

In the last file I tried 在我尝试的最后一个文件中

import conversion
import user_input
print(conversion.conversion(user_input.number()))

and that gave me this error 那给了我这个错误

Traceback (most recent call last):
File "C:/Users/adkir/PycharmProjects/lab_23/main.py", line 4, in <module>
print(conversion.conversion(user_input.number()))
File "C:\Users\adkir\PycharmProjects\lab_23\conversion.py", line 5, in 
conversion
while num // 2 > 0:
TypeError: unsupported operand type(s) for //: 'str' and 'int'

So basically im lost. 所以基本上我输了。 Can anyone point me in the right direction so I can figure this out? 谁能指出我正确的方向,以便我找出答案?

Your conversion function takes num as a parameter and yet it overwrites num 's value by assigning an empty string to it. 您的conversion函数将num作为参数,但是它通过为它分配一个空字符串来覆盖num的值。

Remove the line: 删除行:

num = ''

and it should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM