Python起航

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python起航相关的知识,希望对你有一定的参考价值。


#海龟画圆圈
#number_4.py
import turtle
colors = [purple]
t = turtle.Pen()
turtle.bgcolor(yellow)
for x in range(300):
    #t.pencolor(colors[x%6])
    #t.width(x/100 + 1)
    t.circle(x)
    t.left(91)

 

#绘制旋转七彩图
#
number_1.py import turtle colors = [red, purple, blue, green, yellow, orange] t = turtle.Pen() turtle.bgcolor(black) for x in range(360): t.pencolor(colors[x%6]) t.width(x/100 + 1) t.forward(x) t.left(59)

 

#输入名字输出名字
#Helloworld.py
name = input("What‘s your name?\n")
print("Hello ,",name)
#眩晕图来也~~~
#number_3.py
import turtle
colors = [purple]
t = turtle.Pen()
turtle.bgcolor(yellow)
for x in range(300):
    #t.pencolor(colors[x%6])
    #t.width(x/100 + 1)
    t.forward(x)
    t.left(90)#旋转度数

 

#Python绘图大法!!!
#number_5 一个变量搞定一切.py
import turtle
colors = [yellow, red, purple, green, blue, orange]
sides = eval(input("Enter a number between 2 and 6: "))
t = turtle.Pen()
turtle.bgcolor(black)
for x in range(360):
    t.pencolor(colors[x % sides])
    t.width(x * sides/200)
    t.forward(x * 3/sides + x)
    t.left(360/sides + 20)
#number_7 螺旋式输出名字.py
import turtle
t = turtle.Pen()
turtle.bgcolor(black)
colors = [red, purple, blue, orange]
name = turtle.textinput("Please enter your name", "What‘s your name?")
for x in range(100):
    t.pencolor(colors[x % 4])
    t.penup()
    #t.width(x * sides/200)
    t.forward(x * 4)
    t.pendown()
    t.write(name, font = ("Arial", int((x+4)/4),"bold"))
    t.left(93)
#random函数和坐标
import random
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "gray"]
for n in range(50):
    t.pencolor(random.choice(colors))
    size = random.randint(10, 40)
    x = random.randrange(-turtle.window_width()//2,
                          turtle.window_width()//2)
    y = random.randrange(-turtle.window_height()//2,
                          turtle.window_height()//2)
    t.penup()
    t.setpos(x,y)
    t.pendown()
    for m in range(size):
        t.forward(m * 2)
        t.left(95)

 

#Rock_Paper_Scissors
#RockPaperScissors.py
import random
choices = ["rock", "paper", "scissors"]
print("Rock crushes scissors. Scissors cut paper. Paper covers rock.")
player = input("Do you want to be rock, paper, or scissors (or quit)? ")
while player != "quit":                 # Keep playing until the user quits
    player = player.lower()             # Change user entry to lowercase
    computer = random.choice(choices)   # Pick one of the items in choices
    print("You chose " +player+ ", and the computer chose " +computer+ ".")
    if player == computer:
        print("It‘s a tie!")
    elif player == "rock":
        if computer == "scissors":
            print("You win!")
        else:
            print("Computer wins!")
    elif player == "paper":
        if computer == "rock":
            print("You win!")
        else:
            print("Computer wins!")
    elif player == "scissors":
        if computer == "paper":
            print("You win!")
        else:
            print("Computer wins!")
    else:
        print("I think there was some sort of error...")
    print()                             # Skip a line
    player = input("Do you want to be rock, paper, or scissors (or quit)? ")

 

#选牌游戏
# HighCard.py
import random
suits = ["clubs", "diamonds", "hearts", "spades"]
faces = ["two", "three", "four", "five", "six", "seven", "eight", "nine",
         "ten", "jack", "queen", "king", "ace"]
keep_going = True
while keep_going:
    my_face = random.choice(faces) 
    my_suit = random.choice(suits)
    your_face = random.choice(faces)
    your_suit = random.choice(suits)
    print("I have the", my_face, "of", my_suit)
    print("You have the", your_face, "of", your_suit)
    if faces.index(my_face) > faces.index(your_face):
        print("I win!")
    elif faces.index(my_face) < faces.index(your_face):
        print("You win!")
    else:
        print("It‘s a tie!")
    answer = input("Hit [Enter] to keep going, any key to exit: ")
    keep_going = (answer == "")

 

#五个骰子
# FiveDice.py
import random
# Game loop 
keep_going = True
while keep_going:
    # "Roll" five random dice
    dice = [0,0,0,0,0]          # Set up an array for five values dice[0]-dice[4]
    for i in range(5):          # "Roll" a random number from 1-6 for all 5 dice
        dice[i] = random.randint(1,6)
    print("You rolled:", dice)  # Print out the dice values
    # Sort them
    dice.sort()
    # Check for five of a kind, four of a kind, three of a kind
    # Yahtzee - all five dice are the same
    if dice[0] == dice[4]:
        print("Yahtzee!")
    # FourOfAKind - first four are the same, or last four are the same
    elif (dice[0] == dice[3]) or (dice[1] == dice[4]):
        print("Four of a kind!")
    # ThreeOfAKind - first three, middle three, or last three are the same
    elif (dice[0] == dice[2]) or (dice[1] == dice[3]) or (dice[2] == dice[4]):
        print("Three of a kind")
    keep_going = (input("Hit [Enter] to keep going, any key to exit: ") == "")

    
    
    

 

#万花筒(很好看很对称!!)
#Kaleidoscope.py
import random
import turtle
t = turtle.Pen()
t.speed(0)
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "gray"]
for n in range(50):
    # Generate spirals of random sizes/colors at random locations on the screen
    t.pencolor(random.choice(colors))   # Pick a random color from colors[]
    size = random.randint(10,40)        # Pick a random spiral size from 10 to 40
    # Generate a random (x,y) location on the screen
    x = random.randrange(0,turtle.window_width()//2)
    y = random.randrange(0,turtle.window_height()//2)
    # First spiral
    t.penup()
    t.setpos(x,y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(91)
    # Second spiral
    t.penup()
    t.setpos(-x,y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(91)    
    # Third spiral
    t.penup()
    t.setpos(-x,-y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(91)
    # Fourth spiral
    t.penup()
    t.setpos(x,-y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(91)

 


以上是关于Python起航的主要内容,如果未能解决你的问题,请参考以下文章

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档

python 机器学习有用的代码片段

python 代码片段和解决方案