1.进入到该.py文件的目录下
2.命令输入python xxx.py即可
**f5运行
>>> myteacher = "mr.z"
>>> yourteacher = myteacher
>>> myteacher
'mr.z'
>>> yourteacher
'mr.z'
---------
>>> myteacher = "zz"
>>> myteacher
'zz'
>>> yourteacher
'mr.z'
>>>
改变变量值,会创建新的一个东西。
1.变量名要区分大小写
2.变量名必须以下划线或字母开头
3.变量名不可以存在空格
>>> print (3/2)
1.5
>>> print (5/2)
2.5
>>> print (10/3)
3.3333333333333335
>>> print (4/2)
2.0
>>>
>>> print (10%3)
1
>>>
>>> print (3e5)
300000.0
>>>
>>> a = 21
>>> a
21
>>> b = float(a)
>>> b
21.0
>>> a
21
>>> b
21.0
>>> e = 54.99
>>> e
54.99
>>> f = int(e)
>>> f
54
>>> print (e)
54.99
>>> a = 12
>>> b = "12"
>>> c = 12.0
>>> type(a)
<class 'int'>
>>> type(b)
<class 'str'>
>>> type(c)
<class 'float'>
>>>
>>> input()
hello
'hello'
>>>
#print("please input: ");
#e = input()
e =input("\n please input: ")
print(e)
======= RESTART: E:/python/test-5.py ==========
please input: julia
julia
>>>
注:
1.#为单行注释符
2.\n为换行符
numStars = int(input("how much * do you want?"))
for i in range(1, numStars+1):
print("*")
运行:
====== RESTART: E:/study/python/11-2.py =====
how much * do you want?5
*
*
*
*
*
>>>
如何不换行嘞?
加上end = “ ”
numStars = int(input("how much * do you want?"))
for i in range(1, numStars+1):
print("*", end = " ")
运行:
====== RESTART: E:/study/python/11-2.py =====
how much * do you want?5
* * * * *
>>>
python3.5.2代码如下:
import urllib.request
file = urllib.request.urlopen('http://helloworldbook.com/data/message.txt')
message = file.read()
print (message)
运行成功如下:
========= RESTART: E:/python/test-5.py ==============
b'Congratulations! You have just made your computer reach across the internet to get a secret message!\r\nI hope you are enjoying "Hello World! Computer Programming for Kids and Other Beginners".\r\nHave fun!\r\n\r\nWarren and Carter\r\n\r\n'
>>>
python2.x代码如下:
import urllib
file = urllib . urlopen('http://helloworldbook.com/data/message.txt')
message = file.read()
print message
在python3.5.2中运行2.x的代码会报错
========= RESTART: E:/python/test-5.py =========
Traceback (most recent call last):
File "E:/python/test-5.py", line 2, in <module>
file = urllib . urlopen('http://helloworldbook.com/data/message.txt')
AttributeError: module 'urllib' has no attribute 'urlopen'
>>>
注:
1.urllib已改为urllib.request
2.print要加括弧
首先下载安装easygui
>>> import easygui
>>> easygui.msgbox("hello julia")
enter键后出现下图:
点击‘ok’
python shell中返回‘ok’
>>> import easygui
>>> easygui.msgbox("hello julia")
'OK'
>>>
注:
1.EasyGui的msgbox就是包含一条信息和一个ok按钮的对话框
import easygui
flavor = easygui.buttonbox("what is your favorite ice cream flavor?",
choices = ['vanilla', 'chocolate', 'strawberry' ])
easygui.msgbox("you picked " + flavor)
如果在代码最后加一句print(flavor)
点击‘ok’按钮后返回选择的对应按钮的值
======= RESTART: G:/STUDY/pythonStudy/6.py ======
>>>
======= RESTART: G:/STUDY/pythonStudy/6.py =======
vanilla
>>>
======= RESTART: G:/STUDY/pythonStudy/6.py =======
chocolate
>>>
======= RESTART: G:/STUDY/pythonStudy/6.py =======
strawberry
>>>
>>> import easygui
>>> easygui.buttonbox("hello julia",choices=['a','b','c'])
'a'
>>>
1.
import easygui
flavor = easygui.choicebox("what is your favorite ice cream flavor?",
choices = ['vanilla', 'chocolate', 'strawberry' ])
easygui.msgbox("you picked " + flavor)
print(flavor)
运行:
选择一个,点击‘ok’,把选择的字符传递给下一个框
点击‘ok’,返回‘ok’
===== RESTART: G:/STUDY/pythonStudy/6-2.py =====
OK
vanilla
>>>
若在第一个弹框,选择cancel
则会报错,这是因为程序的最后一行希望得到某个文本(如Vanilla) ,倘若你点击Cancel. 它将得不到任何输入。
===== RESTART: G:/STUDY/pythonStudy/6-2.py =====
Traceback (most recent call last):
File "G:/STUDY/pythonStudy/6-2.py", line 4, in <module>
easygui.msgbox("you picked " + flavor)
TypeError: Can't convert 'NoneType' object to str implicitly
>>>
import easygui
flavor = easygui.enterbox("what is your favorite ice cream flavor?")
easygui.msgbox("you entered " + flavor)
print(flavor)
====== RESTART: G:/STUDY/pythonStudy/6-3.py ======
草莓
>>>
2.enterbox 的默认输入
import easygui
flavor = easygui.enterbox("what is your favorite ice cream flavor?",default = 'vanilla')
easygui.msgbox("you entered " + flavor)
print(flavor)
注:
1.EasyGui的integerbox用来输入整数,但不能输入浮点数
猜数game:
import easygui,random
secret = random.randint(1, 99)
guess = 0
tries = 0
easygui.msgbox("let's guess my secret number!only 6 times")
while guess != secret and tries < 6:
guess = easygui.integerbox("please input your guessnumber:")
if not secret:break
if guess < secret:
easygui.msgbox("too low")
if guess > secret:
easygui.msgbox("too high")
tries = tries + 1
if guess == secret:
easygui.msgbox("congraduations!")
else:
easygui.msgbox("game over")
注意哦:
1.先按照pygame,cmd输入命令:pip install pygame
2.把图片(8张.png)跟代码放同一个文件夹
import pygame, sys, random
# different images for the skier depending on his direction
skier_images = ["skier_down.png", "skier_right1.png", "skier_right2.png",
"skier_left2.png", "skier_left1.png"]
# class for the skier sprite
class SkierClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320, 100]
self.angle = 0
def turn(self, direction):
# load new image and change speed when the skier turns
self.angle = self.angle + direction
if self.angle < -2: self.angle = -2
if self.angle > 2: self.angle = 2
center = self.rect.center
self.image = pygame.image.load(skier_images[self.angle])
self.rect = self.image.get_rect()
self.rect.center = center
speed = [self.angle, 6 - abs(self.angle) * 2]
return speed
def move(self, speed):
# move the skier right and left
self.rect.centerx = self.rect.centerx + speed[0]
if self.rect.centerx < 20: self.rect.centerx = 20
if self.rect.centerx > 620: self.rect.centerx = 620
# class for obstacle sprites (trees and flags)
class ObstacleClass(pygame.sprite.Sprite):
def __init__(self, image_file, location, type):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.location = location
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
def scroll(self, terrainPos):
self.rect.centery = self.location[1] - terrainPos
# create one "screen" of terrain: 640 x 640
# use "blocks" of 64 x 64 pixels, so objects aren't too close together
def create_map(start, end):
obstacles = pygame.sprite.Group()
locations = []
gates = pygame.sprite.Group()
for i in range(10): # 10 obstacles per screen
row = random.randint(start, end)
col = random.randint(0, 9)
location = [col * 64 + 20, row * 64 + 20] #center x, y for obstacle
if not (location in locations): # prevent 2 obstacles in the same place
locations.append(location)
type = random.choice(["tree", "flag"])
if type == "tree": img = "skier_tree.png"
elif type == "flag": img = "skier_flag.png"
obstacle = ObstacleClass(img, location, type)
obstacles.add(obstacle)
return obstacles
# redraw the screen, including all sprites
def animate():
screen.fill([255, 255, 255])
pygame.display.update(obstacles.draw(screen))
screen.blit(skier.image, skier.rect)
screen.blit(score_text, [10, 10])
pygame.display.flip()
def updateObstacleGroup(map0, map1):
obstacles = pygame.sprite.Group()
for ob in map0: obstacles.add(ob)
for ob in map1: obstacles.add(ob)
return obstacles
# initialize everything
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0, 6]
map_position = 0
points = 0
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0
# group for all obstacles to do collision detection
obstacles = updateObstacleGroup(map0, map1)
# font object for score
font = pygame.font.Font(None, 50)
# main Pygame event loop
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN: # check for key presses
if event.key == pygame.K_LEFT: # left arrow turns left
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT: #right arrow turns right
speed = skier.turn(1)
skier.move(speed)
map_position += speed[1] # scroll the terrain
# manage terrain maps, switch between them,
# and create new terrain at the bottom
if map_position >=640 and activeMap == 0:
activeMap = 1
map0 = create_map(20, 29)
obstacles = updateObstacleGroup(map0, map1)
if map_position >=1280 and activeMap == 1:
activeMap = 0
for ob in map0:
ob.location[1] = ob.location[1] - 1280 # wrap around to top
map_position = map_position - 1280 #
map1 = create_map(10, 19)
obstacles = updateObstacleGroup(map0, map1)
for obstacle in obstacles:
obstacle.scroll(map_position)
# check for hitting trees or getting flags
hit = pygame.sprite.spritecollide(skier, obstacles, False)
if hit:
if hit[0].type == "tree" and not hit[0].passed: #crashed into tree
points = points - 100
skier.image = pygame.image.load("skier_crash.png") # crash image
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png") # resume skiing
skier.angle = 0
speed = [0, 6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed: # got a flag
points += 10
obstacles.remove(hit[0]) # remove the flag
score_text = font.render("Score: " +str(points), 1, (0, 0, 0))
animate()