简体   繁体   English

Python没有运行我的程序而没有任何错误

[英]Python not running my program without any error

I need to create a program that will convert Base 10 numbers in Base 2. 我需要创建一个程序来转换Base 2中的Base 10数字。

Next is the code, it can not run as expected even if it has no error: 接下来是代码,即使没有错误,它也无法按预期运行:

E = input('Please enter a number') 
Eint= int(E)
for N in range(100,0):
    while 2**N > Eint:
         N = N-1 
         print(0)  
    if B**N <= Eint:
        Eint = Eint - 2**N 
        print(1)   
    Print('finished')   

When I'm running it it will ask me the number but that's all, thank you for your help guys. 当我运行它时,它会问我这个数字,仅此而已,谢谢您的帮助。

在此处输入图片说明

From a quick inspection, range(100,0) , B , and Print() are three culprits here! 从快速检查来看, range(100,0)BPrint()是这里的三个元凶! If you want to pass through numbers from 0 to 99, then range(100) is what you need. 如果要传递0到99之间的数字,则需要range(100) Now, what is B? 现在,B是什么? Print should be written in lower case: print . 打印应使用小写形式: print

After we fix these syntax errors, let us try to revisit the program and understand what it is supposed to do. 修复这些语法错误之后,让我们尝试重新访问该程序并了解它应该做什么。 Have fun :-) 玩得开心 :-)

EDIT to fix the code in the question: 编辑以修复问题中的代码:

E = input('Please enter a number: ') 
Eint = int(E)
for N in range(8,-1,-1):
    if 2**N > Eint:
        print(0, end='')
    else:
        Eint = Eint - 2**N 
        print(1, end='')

print()
print('finished')

Please note that Python is a language that uses indentations to denote code blocks. 请注意,Python是使用缩进表示代码块的语言。 This code will convert a decimal to binary. 此代码会将十进制转换为二进制。 Now, note that the range start of 8 gives you a hint about the upper bound of the number that the code can translate. 现在,请注意以8开头的范围会提示您有关代码可以转换的数字的上限。 Therefore, an if condition must be added after the second statement to ensure we are not attempting to convert a number that is too large. 因此,必须在第二条语句之后添加if条件,以确保我们不尝试转换太大的数字。 Enjoy! 请享用!

If it helps check my solution too. 如果它也有助于检查我的解决方案。 Because I guess you don't want to see the result on separate lines, so I create a list for you to see the result in one line. 因为我想您不想在单独的行中看到结果,所以我创建了一个列表供您在一行中查看结果。

E = int(input('Please enter a number\n'))
Eint = E
base_two=[]
while E > 0:
    a = int(float(E%2))
    base_two.append(a)
    E = (E-a)/2
base_two.append(0)
string = ""
for j in base_two[::-1]:
    string = string+str(j)
print("Binary for", Eint, "is", string)
print('finished')

I was late a little bit :) 我有点迟了:)

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

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