简体   繁体   English

并排打印两个def函数Python 3

[英]Printing two def function side by side Python 3

I am supposed to write a module that converts meters to feet and feet to meters using a defined function in Python 3. It should display like this: 我应该编写一个模块,该模块使用Python 3中定义的函数将米转换为英尺,并将英尺转换为米。它应显示如下:

Feet       Meters       |    Meters       Feet

1.0         .305        |    20.0          65.574
2.0         .610        |    26.0          81.967
...
9.0         2.745       |    60.0          196.721
10.0        3.050       |    66.0          213.115

I'm supposed to be using two functions: 我应该使用两个函数:

def footToMeters(foot): 

and

def meterToFoot(meter):

The code that I have right now is this: 我现在拥有的代码是这样的:

foot = 1
meter = 20

def footToMeter(foot):
    meters = foot * .305
    print("Feet \t Meters")
    for foot in range(1,11):
        print(foot, "\t", meters)
        foot +=1
        meters = foot * .305


print()

def meterToFoot(meter):
    feet = round((meter/.305),3)
    print("Meters \t Feet")
    for meter in range(20,69,6):
        print(meter, "\t", feet)
        meter += 6
        feet = round((meter/.305),3)


print(footToMeter(foot))
print(meterToFoot(meter))

Obviously the last two print statements simply print the two functions one after the other. 显然,最后两个打印语句只是简单地一个接一个地打印两个函数。

My question is how do I get the two functions two print side by side? 我的问题是如何同时获得两个打印的两个功能?

Any help is much appreciated! 任何帮助深表感谢! Thank you! 谢谢!

Invert your code: the functions should simply take an input and return an output, then be called from a loop that prints values. 反转您的代码:函数应该只接受输入并返回输出,然后从打印值的循环中调用它们。

For example: 例如:

def footToMeter(foot):
    return foot * .305

print("Feet \t Meters")
for foot in range(1,11):
    print(foot, "\t", footToMeter(foot))

Here's the code, but you need to make sure both two tables have the same number of rows. 这是代码,但是您需要确保两个表的行数相同。

Notice I changed meter's ranges from range(20, 69, 6) to range(20, 75, 6) 注意我将仪表的范围从range(20, 69, 6)更改为range(20, 75, 6)

btw I don't have python3, so try change it to python3 yourself:) 顺便说一句,我没有python3,因此请尝试将其自己更改为python3 :)

foot = 1
meter = 20


def footToMeter(foot):
    return foot * .305


def meterToFoot(meter):
    return round((meter/.305),3)


print "Feet \t Meters |\t Meters \t Feet"
for foot, meter in zip(range(1, 11), range(20, 75, 6)):
    print ("%s \t %s \t|\t %s \t %s" 
            % (foot, footToMeter(foot), meter, meterToFoot(meter)))

Unless you use special escape sequences like ANSI the print statement (like old typewriters) writes text on the console from left to right, breaking to the next line after the last char or at the last screen column. 除非使用特殊的转义序列(如ANSI) ,否则打印语句(如旧打字机)将在控制台上从左到右书写文本,并在最后一个字符之后或在最后一个屏幕列处中断到下一行。

To make things work as you want, you must treat the units conversion and the printing problem separately. 为了使事情按您希望的那样工作,您必须分别处理单位转换和打印问题。 Your conversion functions should only return the converted value. 您的转换函数应仅返回转换后的值。 Then you can deal with the printing and formatting on another code block or function. 然后,您可以处理另一个代码块或功能上的打印和格式化。

Here is the code that should work as you expect, using string formatting for a better result: 以下是应该使用字符串格式来获得更好结果的代码,可以按预期工作:

def footToMeter(foot):
    return foot * .305

def meterToFoot(meter):
    return meter / .305

# printing the header
print "Feet\tMeters\t|\tMeters\tFeet\n"

# printing the values
feet, meters = 1, 20
for i in range(1, 11):
    print "%0.3f\t%0.3f\t|\t%0.3f\t%0.3f" % (feet, footToMeter(feet), meters, meterToFoot(meters))
    feet += 1
    meters += 4 + i % 2 * 2

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

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