今天在linux里编写小脚本学习python2,但是出了几个错误
猜数字小脚本:
#This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = raw_input()
number = random.randint(1,20)
print("Well,%s,I am thinking of a number between 1 and 20."%(myName))
while guessesTaken < 6:
print'Take a guess.' # there are four spaces in font of print.
guess =raw_input()
guess = int(guess)
guessesTaken =guessesTaken + 1
if guess < number:
print("Your guess is too Low.")# there are eight space in front print.
if guess > number:
print("Your guess is too high.")
if guess == number:
break
if guess ==number:
guessTaken = str(guessesTaken)
print('Good job,'+myName +'! You guessed my number in '+guessesTaken + 'guesses!')
if guess != number:
number = str(number)
print('Nope.The number I was thinking of was'+ number)
但是大家都知道,python2和3毕竟是有区别的,在运行到第8行和第23行时,分别报出来了错误(上面的第8行修改过了)
第8行原来的代码是:print(‘Well,’+myName+’,I am thinking of a number between 1 and 20.’)
Error1:
root@kali:~# python guessnum.py
Hello! What is your name?
Traceback (most recent call last):
File "guessnum.py", line 8, in <module>
print('Well,'+myName+',I am thinking of a number between 1 and 20.')
TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects
Error2:
Traceback (most recent call last):
File "guessnum.py", line 23, in <module>
print('Good job,'+myName +'! You guessed my number in '+guessesTaken + 'guesses!')
TypeError: cannot concatenate 'str' and 'int' objects
下面分别对错误进行修改:
concatenate 的意思是【连接;嵌套】的意思,builtin:翻译:内置的;内在的;内接的;
想法1:用format()方法进行格式化处理,
print('Well,'{0},I am thinking of a number between 1 and 20.'.format(myName))
很好,不报错了,但是它把打印出来的语句改了
Hello! What is your name?
Well,<built-in function input>,I am thinking of a number between 1 and 20.
替代了让用户输入的名字,这怎么可以,
我认为错了。打印出来的就是字符串了,不在是编码里的变量了,不在具有变量替换功能了。so.这个方法 No pass 不通过。
想法2: %格式化替换法
print("Well,%s,I am thinking of a number between 1 and 20."%(myName))
运行:
Hello! What is your name?
lili
Well,lili,I am thinking of a number between 1 and 20.
可以,成功了。
这里说一个py2和py3的不同,py2中的raw_input() 等同于py3里的input( ),
python2中的raw_input()与python3中的input()功能一样,raw_input()会把用户输入的任何值都作为字符串来对待;而python2中的input不能读取非字符串,
在这里插入图片描述
可能有点看不清,尊重原文,(引用原文: https://blog.csdn.net/mydistance/article/details/85951450 )
解决方法:
解决这个方法只有提前把num转换为字符串类型,可以使用bytes函数把int型转换为string型。https://blog.csdn.net/u012102306/article/details/50851355
guessesTake = bytes(guessesTaken) #改为字符串类型
print('Good job,%s ! You guessed my number in %s guesses!'%(myName,guessesTaken))
正确代码:
# -*- coding: utf8 -*-
#This #This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
#myName = raw_input()
myName = raw_input() # 接收字符串类型
number = random.randint(1,20)
print("Well,%s,I am thinking of a number between 1 and 20."%(myName))
while guessesTaken < 6:
print'Take a guess.' # there are four spaces in font of print.
guess =raw_input()
guess = int(guess)
guessesTaken =guessesTaken + 1
if guess < number:
print("Your guess is too Low.")# there are eight space in front print.
if guess > number:
print("Your guess is too high.")
if guess == number:
break
if guess ==number:
guessTaken = str(guessesTaken)
guessesTake = bytes(guessesTaken) #改为字符串类型
print('Good job,%s ! You guessed my number in %s guesses!'%(myName,guessesTaken))
if guess != number:
number = str(number)
print('Nope.The number I was thinking of was'+ number)
经测试运行成功。
几种Python字符串拼接方法链接https://www.cnblogs.com/bigtreei/p/7892113.html
8 错误和异常(Errors and Exceptions):https://blog.csdn.net/on_1y/article/details/8631204
py2和py3的区别:https://www.cnblogs.com/ttkl/p/9398791.html
https://blog.csdn.net/mydistance/article/details/85951450