Python(一)初学小实验

来源:互联网 发布:mac os 最好的版本 编辑:程序博客网 时间:2024/05/22 14:27

实验

__author__ = 'Frank'

temp = input("input number:")

guess = int(temp)

if guess == 8:

   print("Right is 8")

   print("yea,very good,you won!")

else:

   print("Unfortunate,you are wrong!")

   print("Right is 8")

print("Game Over!!!")

------------------------------------------------------------------------------

实验二:

__author__ = 'Frank'

first = "3"

second = "4"

third = first + second

print(third) #这里的输出结果是34,也就是3和4并列出来的数字,并不是真正的数字“34”

 

first = 3

second = 4

third = first + second

print(third) #这里输入的结果是7,也就是3+4=7的意思,区别于上面的部分。

----------------------------------------------------------------------------

综上所述:print输出常量后面的括号内如果加“”号,

则表示输入双引号内的实际数值,而如果不加双引号(),则括号内的是一个变量。

也就是输出变量的值。

------------------------------------------------------------------------------------------------------------------------

实验三:

__author__ = 'Frank'

print('''line1\nline3''') #这里的\n表示换行

print('temp')

print("c:\\i'am tao") #这是print("")的直接输出内容

print('c:\\i\'am tao') #这里是print('')的单引号,内部需要使用到转义字符,反斜杠\

print("hello\n" * 3) #连续输出3次,没输出一次就换行

那么实验二的结果就是:

line1

line3

temp

c:\i'am tao

c:\i'am tao

hello

hello

hello

----------------------------------------------------------------------------------------------------------------

实验四:

__author__ = 'Frank'

import random

secret = random.randint(1,10)

temp = input("please give me anumber...\n")

guess = int(temp)

count = 0

while guess != secret:

    if count== 0 and guess > secret:

       print("it's too big!")

    if count== 0 and guess < secret:

       print("it's too small!")

    temp =input("please give me a number again...\n")

    guess =int(temp)

    count +=1

    if count> 2:

       print("because you are wrong by four,so break. ")

       break

    if guess== secret:

       print("yes,you are right!")

        print("haha~~~")

    else:

        ifguess > secret:

           print("it's very big!")

       else:

           print("it's very small!")

print("Game Over!!!")

#第2行是调用random的随机函数库
#第3行是指定random的随机函数库的取值的范围randint(1,10)
#第6行的count = 0是指的计数,也就是输入数字的次数。而相对的第14行的conut += 1的意思是count= conut + 1,
#即每输入一次count的值就会+1

--------------------------------------------------------------------------------------------------------------------------------

0 0
原创粉丝点击