Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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,我的代码: sentence = 'I am honest!I do!I do!I really do!' amount=input("Please input the number of '!'(s) that you want to print before stop printing? ") for j in sentence: if amount='1': if j == '!':

我的代码:

sentence = 'I am honest!I do!I do!I really do!'
    amount=input("Please input the number of '!'(s) that you want to print before stop printing? ")
    for j in sentence:
        if amount='1':
            if j == '!':
                print(j)
                break
            else:
                print(j, end='')
        if amount='2':
我试过了,但是如果输入的金额是2,我想不出任何方法可以起作用……就像我搜索j=='!'若要继续,它将得出与amount==1时完全相同的结果

预期产出:

Please input the number of '!'(s) that you want to print before stop printing? 3
I am honest!
I do!
I do!

谢谢你的帮助

你不远。您只需将金额转换为整数,并将其用作计数器:

sentence = 'I am honest!I do!I do!I really do!'
amount=input("Please input the number of '!'(s) that you want to print before stop printing? ")
amount = int(amount)
for j in sentence:
    if amount == 0:
        break
    if j == '!':
        amount -= 1
        print(j)
    else:
        print(j, end='')

每次不是1和
j=='!'。最终,您的循环将达到
amount==1
,并且您的循环将根据需要退出。感谢您的帮助!但我想知道是否有办法做到这一点-->如果用户输入的金额大于1,系统可以感知数字并打印出感叹号的相应数字?还是你在上面写的?