我有一个关于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的问题(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)
但是,我想知道是否还有其他选择.
我有一个简单的问题,
是IndentationError
一个SyntaxError
在Python或不?
我想不是,但由于我是初学者,我想确定.语法错误只是那些让我SyntaxError
在解释器中作为响应的错误吗?例如,如果我输入
3f = 22
Run Code Online (Sandbox Code Playgroud)
我明白了
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
所以,如果有其他东西(IndentationErro
r等),它可能是一个子类型SyntaxError
或不是?
我有一个关于python的问题.
我有变量a
,b
,c
和d
.
我有以下几行:
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)
是否可以缩短此代码?
谢谢!
我在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并且不应该执行.但由于某种原因,它仍然存在.
你能告诉我我做错了什么吗?
谢谢!
python ×4
python-3.x ×3
c ×1
isinstance ×1
list ×1
printing ×1
string ×1
syntax ×1
while-loop ×1