python输入方式:
username=raw_input("what‘s your name") message=raw_input("message") while message!="exit": print(username+" : "+message) message=raw_input("Enter a message")
结果显示:
================= RESTART: /home/xiaozhi/Python/Raw_Input.py =================
what‘s your namelxj
messagehello
lxj : hello
Enter a messagemp
lxj : mp
Enter a messagedel
lxj : del
Enter a messagehah
lxj : hah
Enter a message
number1=eval(input("Enter the first number")) number2=eval(input("Enter the second number")) number3=eval(input("Enter the third numer")) average=(number1+number2+number3)/3 print("the average of ",number1,number2,number3,average)
结果显示
================= RESTART: /home/xiaozhi/Python/Raw_Input.py =================
Enter the first number"10"
Enter the second number"20"
Enter the third numer"30"
(‘the average of ‘, 10, 20, 30, 20)
//eval只接受字符串
if..elif..else语句的使用
print("a:best b:good c:normal d:bad e:worst,Enter your option") s=input("Please input ") if s=="a": print("you‘re best") elif s=="b": print("you‘re good") elif s=="c": print("you‘re normal") elif s=="d": print("you‘re bad") elif s=="e": print("you‘re worst") else: print("your input isn‘t exist")
结果显示:
================== RESTART: /home/xiaozhi/Python/Turtle.py ==================
a:best b:good c:normal d:bad e:worst,Enter your option
Please input "a"
you‘re best
Turtle模块,模块是预先写好的函数集合,可以引入到你的Python程序中,它们可以让你的程序完成一些没有这个模块一般很难做到的事情
import turtle >>> turtle.forward(100) >>> turtle.right(90) >>> turtle.forward(100) >>> turtle.right(90) >>> turtle.forward(100) >>> turtle.right(90) >>> turtle.forward(100)
结果显示为一个正方形
import turtle length=20 angle=45 count=0 while count<5: turtle.forward(length) turtle.right(angle) turtle.forward(length) turtle.left(angle) count+=1
//画图和while语句的使用
1. if 语句
i = 10 n = int(raw_input()) n == i: n < i: :
2. while语句
True: :
3. for 循环 for..in
i range(0, 5): i
注:当for循环结束后执行else语句;
range(a, b)返回一个序列,从a开始到b为止,但不包括b,range默认步长为1,可以指定步长,range(0,10,2);
注:空格的重要性,缩进表示代码块
空白在python是非常重要的,行首的空白是最重要的,又称为缩进。行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而决定语句
分组。这意味着同一层次的语句必须有相同的缩进,每一组这样的语句称为一个块。
注意:不要混合使用空格和制表符来缩进,因为在跨越不同的平台时无法正常工作。
画圆
import turtle count=0 while count<360: turtle.forward(1) turtle.right(1) count+=1
import turtle turtle.circle(45)
拓展//移动笔的坐标,即turtle对象
import turtle turtle.color("red") turtle.penup() turtle.goto(0,50) turtle.pendown() turtle.circle(45)
画6边形
import turtle sides=6 angle=360.0/sides length=400.0/sides for side in range(sides): turtle.forward(length) turtle.right(angle) turtle.done()
填充圆内颜色
import turtle turtle.fillcolor("blue") turtle.begin_fill() turtle.circle(100) turtle.end_fill()
在填充之前选择填充颜色,开始填充,画圆,结束填充
5个圆
import turtle x=0 count=0 radis=100 while count<5: turtle.penup() turtle.goto(x,0) turtle.pendown() turtle.fillcolor("red") turtle.begin_fill() turtle.circle(radis) turtle.end_fill() count+=1 x+=radis radis-=20