Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中打印输入的奇数字母_Python - Fatal编程技术网

如何在python中打印输入的奇数字母

如何在python中打印输入的奇数字母,python,Python,我必须为学校做这件事,但我做不出来。我必须得到一个输入,只打印奇数字符。到目前为止,我已经把输入放在一个列表中,我有一个while循环,这是任务表上的一个线索,但我无法计算出来。请帮助: inp = input('What is your name? ') name = [] name.append(inp) n=1 while n<len(name): 我想这就是你所需要的你不需要把字符串放在一个列表中,字符串本质上已经是一个更正式的字符列表,它是一个序列 您可以为此使用索引和模数运

我必须为学校做这件事,但我做不出来。我必须得到一个输入,只打印奇数字符。到目前为止,我已经把输入放在一个列表中,我有一个while循环,这是任务表上的一个线索,但我无法计算出来。请帮助:

inp = input('What is your name? ')
name = []
name.append(inp)
n=1

while n<len(name):

我想这就是你所需要的

你不需要把字符串放在一个列表中,字符串本质上已经是一个更正式的字符列表,它是一个序列

您可以为此使用索引和模数运算符%


我知道这看起来可能不一样,但看看这个问题。它很有用,因为OP使用模运算%2,这是一种常见的方法,而接受的答案使用切片,可以用于字符串和列表。你可以投票给dupe目标,这是接受的答案:PWow-你赢得了这轮python golffixed。。。我还反复地说:P以愚人的身份结束……回答很好:而且比我的更具教育意义+1如果不是请求硬件帮助,我会像你那样做:
print inp[1::2]
inp = input('What is your name? ')
n = 0   # index variable

while n < len(inp):
    if n % 2 == 1:      # check if it is an odd letter
        print(inp[n])   # print out that letter
    n += 1