Python把它变成一个循环,这样看起来像我的照片?

2024-10-02 18:18:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用循环来写这个程序

http://imgur.com/CGRMn

这是我到目前为止,但我的程序必须看起来准确和图片???你知道吗

def main():

    sum=0.0
    for i in range(1,6):
        x,y=eval(input("Please enter length and width of room:"))
        sf=(x*y)
        sum=sum+sf
        print("The total square footage is",sum)

main()

谢谢你的帮助。。。你知道吗


Tags: in程序comhttpforinputmaindef
1条回答
网友
1楼 · 发布于 2024-10-02 18:18:22

只需在print语句上删除缩进。你知道吗

sum=0.0
for i in range(1,6):
    x,y=eval(input("Please enter length and width of room:"))
    sf=(x*y)
    sum=sum+sf
print("The total square footage is",sum)

编辑:

这是python中的一种常用技术,您可以使用它来实现所需的功能:

sum=0.0
for i in range(1,6):
    x,y=eval(input("Please enter length and width of room %i:" % i ))
    sf=(x*y)
    sum=sum+sf
print("The total square footage is %i" % sum )

我在这里做的是在字符串中间放置一个通配符,然后传递参数%我告诉%操作符你要插入一个整数。如果要添加字符串,也可以放置“%s”。还有几个你可以查。这是控制台的另一个示例:

>>> user_name = 'mauricio'
>>> sum = 42
>>> line_to_print = 'Hello user %s, your sum is %i' % (user_name, sum)
>>> print(line_to_print)
Hello mauricio, your sum is 42

相关问题 更多 >