Python 练手

早就想学学写 Python 了,最近看了几篇文档,试着写了几段:

第一个是简单的随机数生成:

1
2
3
import random
number = random.randint(1,30)
print number


第二个是上面那个改的,稍微复杂一点点,猜数字 = =!

1
2
3
4
5
6
7
8
9
10
11
12
import random
number = random.randint(-1000,1000)
running = True
while running:
    guess = int(raw_input('Enter an integer:'))
    if guess == number:
        print 'Congratulations, you guessed it.'
        running = False
    elif guess < number:
        print 'No, it is higher than that.'
    else:
        print 'No, it is lower than that.'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.