在Python中扭转输出的问题 - 学生问题

问题描述 投票:1回答:2

我正在尝试做我的python编程课的第2课,我遇到了一个误解。这本书要求我“延伸到反向输出”。

代码应该像这样输出(假设输入99,3.77,z,你好)

Enter integer (0 - 155):
99
Enter float:
3.77
Enter character:
z
Enter string:
Howdy
99 3.77 z Howdy
Howdy z 3.77 99

但是,当我运行代码时,我收到此错误:

Enter integer (0 - 155):
Enter float:
Enter character:
Enter string:
99 3.77 z Howdy 

Enter integer (0 - 155):
Traceback (most recent call last):
  File "main.py", 

这是我的代码实际上是这样的:

# FIXME (1): Finish reading other items into variables, then output the four 
values on a single line separated by a space

userInt = int(input('Enter integer (0 - 155):\n'))
userFloat = float(input('Enter float:\n'))
userCharacter = str(input('Enter character:\n'))
userString = str(input('Enter string:\n'))
print(userInt, userFloat, userCharacter, userString, '\n')   

# FIXME (2): Output the four values in reverse

userInt = int(input('Enter integer (0 - 155):\n'))
userFloat = float(input('Enter float:\n'))
userCharacter = str(input('Enter character:\n'))
userString = str(input('Enter string:\n'))
print(userInt, userFloat, userCharacter, userString, userString, 
userCharacter, userFloat, userInt)

我尝试了一些不同的东西,但无论我做什么,它都会在'Howdy'之后突出显示空格然后突出显示返回然后打印出该追溯错误消息。我不知道这意味着什么,我不知道如何解决它。非常感谢任何输入!

python reverse
2个回答
0
投票

这是输入输出两次:

# FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space

userInt = int(input('Enter integer (0 - 155):\n'))
userFloat = float(input('Enter float:\n'))
userCharacter = input('Enter character:\n')
userString = input('Enter string:\n')
print(userInt, userFloat, userCharacter, userString, '\n')   

# FIXME (2): Output the four values in reverse

userInt = int(input('Enter integer (0 - 155):\n'))
userFloat = float(input('Enter float:\n'))
userCharacter = input('Enter character:\n')
userString = input('Enter string:\n')
print(userInt, userFloat, userCharacter, userString,'\n'+userString, 
userCharacter, userFloat, userInt)

示例输出:

Enter integer (0 - 155):
99
Enter float:
3.77
Enter character:
z
Enter string:
Howdy
99 3.77 z Howdy 

Enter integer (0 - 155):
99
Enter float:
3.77
Enter character:
z
Enter string:
Howdy
99 3.77 z Howdy 
Howdy z 3.77 99

然后一次:

userInt = int(input('Enter integer (0 - 155):\n'))
userFloat = float(input('Enter float:\n'))
userCharacter = input('Enter character:\n')
userString = input('Enter string:\n')
print(userInt, userFloat, userCharacter, userString,'\n'+userString, 
userCharacter, userFloat, userInt)

示例输出:

Enter integer (0 - 155):
99
Enter float:
3.77
Enter character:
z
Enter string:
Howdy
99 3.77 z Howdy 
Howdy z 3.77 99

0
投票

我得到了一个输出,它一次循环,然后第二次打印正向和反向。

它可能与这条线没有在评论values on a single line separated by a space吗?

(xenial)vash@localhost:~/python/AtBS$ python3.7 verbose.py 
Enter integer (0 - 155):
99
Enter float:
3.77
Enter character:
z
Enter string:
Howdy
99 3.77 z Howdy 

Enter integer (0 - 155):
99
Enter float:
3.7
Enter character:
z
Enter string:
Howdy
99 3.7 z Howdy Howdy z 3.7 99
© www.soinside.com 2019 - 2024. All rights reserved.