(Python)为什么我的输出没有反转? [英] (Python) why is my output not reversing?

查看:118
本文介绍了(Python)为什么我的输出没有反转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,



我是学生并为我的考试做练习。我将不得不创建一个将输入转换为二进制的程序。



到目前为止,我得到的代码工作但是我需要输出的顺序相反。



我使用

 [::  -   1 ]  


谢谢!



我尝试过:



  def  binary_converter( num):
while num!= 0
output = num%< span class =code-digit> 2 0或1
num = num // 2
reverse = str(输出) 更改为字符串
print (反向[:: - 1 ],结束= 反向不工作

print 输入一个数字:
num = int(input())
binary_converter(num)

解决方案

输出总是只是一个字母的字符串(因为它在循环中),所以将其反转只需给出相同的字符串。



你需要做的是,在打印 之前将整个字符串翻转:

  def  binary_converter(num):
reverse =
while num!= 0
输出= num% 2 0或1
num = num // 2
反向+ = str(输出)
print (反向[:: - 1 ],end =

Hey all,

I'm a student and doing a practice for my exam. I will have to create a program that converts an input into binary.

So far, I got the code working BUT I need my output to be in reverse order.

I used

[::-1]

yet it doesn't seem to be doing it.

Thanks!

What I have tried:

def binary_converter (num):
    while num != 0:
        output = num % 2 #0 or 1
        num = num // 2
        reverse = str(output) #change into string
        print (reverse[::-1], end="") #reverse not working

print ("Enter a number:")
num = int(input())
binary_converter(num)

解决方案

output is always just a one-letter string (because it's inside the loop), so reversing it just gives the same string.

What you need to do, is reversing the whole string before printing it:

def binary_converter(num):
    reverse = ""
    while num != 0:
        output = num % 2 # 0 or 1
        num = num // 2
        reverse += str(output)
    print (reverse[::-1], end="")


这篇关于(Python)为什么我的输出没有反转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆