0
Follow
0
View

Python line break problem

silence1227 注册会员
2023-02-27 11:38

Please accept, thank you!

# 输出方式一,将整数转为字符串输出
print(str(4+4)+'\n'+str(8)+'\n'+str(2*4)+'\n'+str(9-1))

# 输出方式二,使用f和花括号
print(f'\n')
print(f'{4+4}\n{8}\n{2*4}\n{9-1}')

img

jake7866891 注册会员
2023-02-27 11:38

This is a syntax error. The correct approach is to separate sections with commas and specify newlines as output intervals

print(4+4,8/1,2*4,9-1,sep='\n')

dxwwdm 注册会员
2023-02-27 11:38

Strings work, but not in the middle of constant expressions

duyuetiancai 注册会员
2023-02-27 11:38

The little girl refers to bing and GPT for some content tuning :
Because Python is a programming language, its syntax is strict and cannot be written at will. In the above code, there is a syntax error in 4+4m8/1Wm2*4Wng9-1, so Python cannot parse correctly and return the result.

To print the four 8s correctly, it needs to be expressed in proper Python syntax, which can be done using the print function as follows:

print(8, 8, 8, 8)

The code above prints four eights, each separated by a space.
The answer is not easy, so take it.

About the Author

Question Info

Publish Time
2023-02-27 11:38
Update Time
2023-02-27 11:38

Related Question

怎么修改代码呢?刚学python

Python输入一段话,怎么显示拼音?

Python可视化图表,为啥会有这种报错

python提示object has no attribute 'to_CSV'

python手机环境怎么实时文字转语音

python tkinter运行时grid函数直接使用不执行但是作为按钮的命令正常执行

使用python将TXT重新排列为CSV文件

关于#python#的问题:并反转剩余列表,为什么输出反转列表显示None呢

多线程化一个Selenium python脚本

python输入名字时候遇到了个问题