Python语法错误:对象不可下标

Python语法错误:对象不可下标,python,syntax-error,Python,Syntax Error,这是我目前的代码: x = int(3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679) q = int(input("How many digits of Pi would you like to Print?")) print(x[1:y]) 抱歉,如果代码缩进不起作用,我不明白 你看,我知道它不能将字符串变量作为文本项转换成整数,但

这是我目前的代码:

x = int(3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679)

q = int(input("How many digits of Pi would you like to Print?"))

print(x[1:y])
抱歉,如果代码缩进不起作用,我不明白

你看,我知道它不能将字符串变量作为文本项转换成整数,但是从这里我该怎么做呢

该代码应该将Pi打印为选定数量的字符

您可以利用此任务。要获得可变数量的小数,只需参数化该值

from decimal import Decimal
x = decimal(3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679)

q = int(input("How many digits of Pi would you like to Print?"))

print("{0:.{width}f}".format(x, width=q))

请注意,print语句中的
width
变量是您的用户输入。还请注意,我将
pi
的定义从
int
更改为
decimal
(因为
float
失去精度)

示例:

How many digits of Pi would you like to Print?5
3.14159

How many digits of Pi would you like to Print?10
3.1415926536

How many digits of Pi would you like to Print?25
3.1415926535897931159979635
试试下面的代码

 x = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"

y = int(input("How many digits of Pi would you like to Print?"))

print(x[1:y+1])

不能将整数用作列表。但由于python的强大功能,您可以渲染字符串。还要注意变量名。最后,因为python开始从0到n计数,所以第n个数字必须是n+1位。

错误消息清楚-您不能下标
int
variableAlso,为什么要将
pi
设为int。无论您在这里做什么,它都会打印
3.0000…
。这
y
从哪里来代替
q
?!使用
y
q