Python自学笔记(问题集)

Python自学笔记(问题集)

“hello world”

print "hello world"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello world")?

错误:SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(“hello world”)?
答案:我下的版本是python3.7,而python2与3的语法已经不太一样了,以上是Python2的语言,以下正确形式。

print("hello world")
hello world

猜数字游戏

import random
secret=random.randint(1,99)
guess=0
tries=0
print("ANOY!I'm the Dread Pirate Roberts,and i have a secret")
print("It is a number from 1 to 99.I'll give you 6 tires.")
while guess !=secret and tries < 6:
    guess = input("what's yer guess?")
    if guess < secret:
        print("too low,ye scurvy dog!")
    elif guess > secret:
        print("too high,landlubber!")
    tries=tries+1
if guess==secret:
    print("Avast!ye got it!found my secret,ye did!")
else:
    print("no more guesses!better luck next time,matey!")
    print("the secret number was",secret)

错误:TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
答案:因为input()返回的数据类型是str类型,必须先把他转换成整数型才可以做比较,使用int( ).

guess = int(input("what's yer guess?"))

你可能感兴趣的:(Python自学笔记(问题集))