小编Tur*_*rbo的帖子

为什么在Python中打印需要三个撇号?

我在Python 3.3.2中制作了这个毕达哥拉斯定理计算器.

我在几行上打印,以便我可以制作一个图表:

print("Welcome to the Pythagoras Theorem Calculator, powered by Python!")
print("Below are the values a, b and c. You will need to input these values after.")
print('''
      | .
      |   .
      |     .
side a|       . side c
      |         .
      |           .
      |_____________.
          side b
      ''')
Run Code Online (Sandbox Code Playgroud)

如上所示,需要三个撇号而不是语音标记.为什么会这样?这是逃脱角色吗?(我尝试在Google上搜索:http://bit.ly/15a4zes)

python

4
推荐指数
1
解决办法
6052
查看次数

循环并附加名称列表

我正在编写一个程序,一直要求用户输入名称,直到输入单词END,此时它会打印出名称列表.

代码:

import getpass
import time
import sys
print("Welcome " + getpass.getuser() + "...")
time.sleep(0.25)
print("This program, powered by Python, it will ask you to enter names...")
time.sleep(0.5)
print("...once you have finished, enter END to print off your list")
names = []
for i in names:
    name = input("Please enter a name: ")
    if name == "END":
        print(names)
        sys.exit()
    names.append(name)
Run Code Online (Sandbox Code Playgroud)

问题是程序在尝试执行for循环之前退出.

为什么会发生这种情况,我该如何解决?

python for-loop python-3.x

3
推荐指数
1
解决办法
1870
查看次数

记录while循环运行的次数? - 蟒蛇

想象一下:

你有一个while循环

你想知道它运行了多少次

你该怎么办???

现在我听到你说,背景是什么?

上下文: 我正在用Python编写这个程序,它考虑的是1到100之间的数字,你要猜测它.猜测参与了一个while循环(请看下面的代码),但我需要知道有多少猜测.

所以,这就是我需要的:

print("It took you " + number_of_guesses + " guesses to get this correct.")
Run Code Online (Sandbox Code Playgroud)

这是Gist的完整代码:https://gist.github.com/anonymous/1d33c9ace3f67642ac09

请记住:我使用的是Python 3x

提前致谢

python loops

2
推荐指数
2
解决办法
2万
查看次数

以相反顺序打印列表不起作用 - Python 3x

我正在编写一个程序,不断要求用户输入名称,直到输入单词 END 为止,此时它会以相反的顺序打印出名称列表。

起初,我不知道如何以相反的顺序打印出列表,所以我发现了这个:Travers a list inverse order in Python

我决定使用reversed()内置函数:

import getpass
import time
import sys
print("Welcome " + getpass.getuser() + "...")
time.sleep(0.25)
print("This program, powered by Python, it will ask you to enter names...")
time.sleep(0.5)
print("...once you have finished, enter END to print off your list")
names = []
while True:
    name = input("Please enter a name: ")
    if name == "END":
        print(reversed(names))
        sys.exit()
    names.append(name)
Run Code Online (Sandbox Code Playgroud)

然而,它打印的只是:

<list_reverseiterator object at 0x0000000002A14F28>
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况?我该如何解决这个问题?

非常感谢

python list

2
推荐指数
1
解决办法
1466
查看次数

Python中的两个小数?

我正在尝试制作一个计算气缸体积和表面积的程序; 我目前正在编写它的音量部分.但是,在输出屏幕中,有两位小数.表明:

气缸容积为193019.2896193019.2896cm³

为什么有两个?

之后,我试图让程序询问用户用户想要多少小数位(dp).我怎样才能做到这一点?

这是当前的代码:

print("Welcome to the volume and surface area cylinder calculator powered by Python!")
response = input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ")
if response=="vol" or response =="SA":
    pass
else:
    print("Please enter a correct statement.")
    response = input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ")

if response=="vol":
    #Below splits 
    radius, height = [float(part) for part in input("What is the …
Run Code Online (Sandbox Code Playgroud)

python decimal

0
推荐指数
1
解决办法
349
查看次数

令人困惑的python - 无法将字符串转换为float

我有一个值错误,即使我尝试使用代码,它也不起作用!

我该怎么做对吗? - 我正在使用Python 3.3.2!

这是代码: 码

如您所见,该程序会询问您可以行走多少英里,并根据您输入的内容为您提供响应.

这是文本格式的代码:

print("Welcome to Healthometer, powered by Python...")
miles = input("How many miles can you walk?: ")
if float(miles) <= 0:
    print("Who do you think you are?!! Go and walk 1000 miles now!")
elif float(miles) >= 10:
    print("You are very healthy! Keep it up!")
elif float(miles) > 0 and miles < 10:
    print("Good. Try doing 10 miles")
else:
    print("Please type in a number!")
    miles = float(input("How many miles can you walk?: "))
    if miles …
Run Code Online (Sandbox Code Playgroud)

python error-handling

-8
推荐指数
1
解决办法
6万
查看次数

标签 统计

python ×6

decimal ×1

error-handling ×1

for-loop ×1

list ×1

loops ×1

python-3.x ×1