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

时间:2018-09-11 03:34:39

标签: python reverse

我正在尝试上python编码类的第2课,但遇到了误会。这本书要求我“还扩展为反向输出。”

代码应该这样输出(假设输入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

但是,当我运行代码时,出现此错误:

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”之后突出显示一个空格,然后突出显示一个返回值,然后打印出该追溯错误消息。我不知道这是什么意思,我不确定如何解决。非常感谢您的输入!

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

Usa
USA, Gender
USA, Age
USA, Civil_Status
USA, Gender, Age
USA, Gender, Civil_Status
.....................
West, Gender
West, Age
.....................

答案 3 :(得分:0)

user_int = int(input('Enter integer (32 - 126):\n'))

user_float = float(input('Enter float:\n'))

user_char = input('Enter character:\n')

user_str = input('Enter string:\n')

print(user_int, user_float, user_char, user_str)

print(user_str, user_char, user_float, user_int)

print(user_int,'converted to a character is',chr(user_int))

答案 4 :(得分:-1)

确保 '\n' 之间有 userString,'\n'+userString,