小编Som*_*One的帖子

Python:for循环 - 在同一行上打印

我有一个关于for在Python 3中使用循环在同一行上打印的问题.我搜索了答案,但我找不到任何相关的.

所以,我有这样的事情:

def function(variable):

    some code here

    return result


item = input('Enter a sentence: ')

while item != '':
    split = item.split()
        for word in split:
            new_item = function(word)
            print(new_item)
    item = input('Enter a sentence: ')
Run Code Online (Sandbox Code Playgroud)

当用户输入句子"短句"时,该函数应该对它做一些事情并且它应该打印在同一行上.假设函数将't'添加到每个单词的末尾,因此输出应为

在短期内判刑

但是,目前的输出是:


短期内
判刑

如何轻松地在同一行上打印结果?或者我应该制作一个新的字符串

new_string = ''
new_string = new_string + new_item
Run Code Online (Sandbox Code Playgroud)

它是迭代的,最后我打印new_string?

python string python-3.x

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

Python:for循环内部print()

我有一个关于Python的问题(3.3.2).

我有一个清单:

L = [['some'], ['lists'], ['here']] 
Run Code Online (Sandbox Code Playgroud)

我想使用以下print()函数打印这些嵌套列表(每个列表在新行上):

print('The lists are:', for list in L: print(list, '\n'))
Run Code Online (Sandbox Code Playgroud)

我知道这是不正确的,但我希望你明白这个想法.你能告诉我这是否可行?如果有,怎么样?

我知道我可以这样做:

for list in L:
    print(list)
Run Code Online (Sandbox Code Playgroud)

但是,我想知道是否还有其他选择.

python printing list python-3.x

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

IndentationError是否是Python中的语法错误?

我有一个简单的问题,

IndentationError一个SyntaxError在Python或不?

我想不是,但由于我是初学者,我想确定.语法错误只是那些让我SyntaxError在解释器中作为响应的错误吗?例如,如果我输入

3f = 22 
Run Code Online (Sandbox Code Playgroud)

我明白了

SyntaxError: invalid syntax 
Run Code Online (Sandbox Code Playgroud)

所以,如果有其他东西(IndentationError等),它可能是一个子类型SyntaxError或不是?

python syntax python-3.x

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

Python:检查变量的类型

我有一个关于python的问题.

我有变量a,b,cd.

我有以下几行:

if    not isinstance(a, int)   or not isinstance(b, int)  \
   or not isinstance(c, int)   or not isinstance(d, int)  \
   or not isinstance(a, float) or not isinstance(b, float)\
   or not isinstance(c, float) or not isinstance(d, float):
    do something
Run Code Online (Sandbox Code Playgroud)

是否可以缩短此代码?

谢谢!

python isinstance

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

while循环与||无法正常工作

我在C中有以下代码:

int main() {

    int num;

    printf("Enter 1, 2 or 3:\n");
    scanf("%d", &num);

    while(num != 1 || num != 2 || num != 3) {
        printf("Try again!\n");
        scanf("%d", &num);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

无论我输入什么,while循环都不会退出.这段代码有什么问题?

当我输入num = 3时,while循环应该被评估为false并且不应该执行.但由于某种原因,它仍然存在.

你能告诉我我做错了什么吗?

谢谢!

c while-loop

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

标签 统计

python ×4

python-3.x ×3

c ×1

isinstance ×1

list ×1

printing ×1

string ×1

syntax ×1

while-loop ×1