|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编写程序:hello.py,要求用户输入姓名并打印“你好,姓名!”
我原本尝试写出的程式码如下:
temp = input("請輸入姓名:")
name = int(temp)
print("你好," + name + "!")
run出结果如下:
Traceback (most recent call last):
File "C:/Users/user/Desktop/python 課後作業/02課練習1.py", line 3, in <module>
print("你好," + name + "!")
TypeError: can only concatenate str (not "int") to str
所以我就参考了小甲鱼的正解,发现不用加上name = int(temp)
我就回到第二课的课堂作业把程式码中的guess = int(temp)给改了
print("123")
guess = input ("guess number:")
if guess == 8:
print("yes")
else:
print("sorry~wrong , the answar is 8")
print("end")
发现程式还是可以运行,但无论我输入的数字正确与否,程式反馈结果都是我输入错误
============= RESTART: C:\Users\user\Desktop\python 課後作業\02課.py =============
123
guess number:5
sorry~wrong , the answar is 8
end
>>>
============= RESTART: C:\Users\user\Desktop\python 課後作業\02課.py =============
123
guess number:8
sorry~wrong , the answar is 8
end
我自己的理解是因为课后作业中姓名为中文,不须整形即代表单一意义;而课堂程式中数字需要整形int( )使其意义固定下来,但不知是否正确
请问有大神可以给新手小弟我讲解整形的使用时机吗?
name = int(temp) 你理解这句是什么意思不?在用一个函数前要知道它是什么,不要随便用
temp = input("請輸入姓名:") # 接收用户输入 永选是字符串
name = int(temp) # 字符串转int 再给name
print("你好," + name + "!") # 好了,字符串 + int +字符串?????这样可以?难道不是 字符串 + 字符串+字符串
|
|