1. 程式人生 > >python學習4——提問

python學習4——提問

type trac ear rec ror efi 結果 fin 不能

一、打印出改變了的輸出。

因為沒有任何計算機基礎,所以出錯很多,摸索了一會。

出錯1:

print("How old are you?"),
age = raw_input()
print("What is your name?"),
name = raw_input()
print("How tall are you?"),
height = raw_input()
print("So your name is %r, you are %r years old and %r tall." , %name,age,height)

輸出結果

E:\abc>python 6.py
How old are you?

Traceback (most recent call last):
File "6.py", line 2, in <module>
age = raw_input()
NameError: name ‘raw_input‘ is not defined

原因:python3中將raw_input()命令改為input(),所以不能會有未定義錯誤。

出錯2:

print("How old are you?"),
age = input()
print("What is your name?"),
name = input()
print("How tall are you?"),

height = input()
print("So your name is %r, you are %r years old and %r tall." , %name,age,height)

輸出結果

E:\abc>python 6.py
File "6.py", line 7
print("So your name is %r, you are %r years old and %r tall.", %name,age,height)

^

SyntaxError: invalid syntax

原因:

%符號前面不應該有:逗號,

出錯3:

print("How old are you?"),
age = input()
print("What is your name?"),
name = input()
print("How tall are you?"),
height = input()
print("So your name is %r, you are %r years old and %r tall." %name,age,height)

輸出結果

E:\abc>python 6.py
How old are you?
20
What is your name?
jack
How tall are you?
189
Traceback (most recent call last):
File "6.py", line 7, in <module>
print("So your name is %r, you are %r years old and %r tall." %name,age,height)
TypeError: not enough arguments for format string

原因:

% 後定義的變量需要用()引用

最終正確的代碼如下:

print("How old are you?"),
age = input()
print("What is your name?"),
name = input()
print("How tall are you?"),
height = input()
print("So your name is %r, you are %r years old and %r tall." %(name,age,height))

輸出結果:

E:\abc>python 6.py
How old are you?
18
What is your name?
lucy
How tall are you?
168
So your name is ‘lucy‘, you are ‘18‘ years old and ‘168‘ tall.

python學習4——提問