#Randomisation
'''
#python 的 module 有已经创建的,也可以自己创建
Ex:my_module.py
pi = 3.1415926
在day4.py 中可import
import my_module
print(my_module.pi)
'''
import random
#包括1 and 10
randomIntegeter = random.randint(1,10)
print(randomIntegeter)
#random.random 随即生成0.0 - 1.0 的小数,不包含0 and 1。若想增大范围,可用乘法
randomFloat = random.random()*5
print(randomFloat)
love_score = random.randint(1,100)
print(f"Your love_score is {love_score}")
#4.1 Heads or Tails Exercise
import random
random_side = random.randint(0,1)
if random_side == 1:
print("Heads")
else:
print("Tails")
#List
offset or shift == 0,so first-states_of_america[0]
positive index or negative index:-1 is the last item in the list ,because you can;t really have minus zero
states_of_america["Delaware","pencilvania"
#alter any item inside the list
states_of_america[1]="Pencilvania"
#add to the list:append--add a single item to the end of the list
states_of_america.append("angelalan")
#extend the states_of_america list
states_of_america.extend(["Angelalan","Jack Baure Land"])
#4.2 Banker Roulette-Who will pay the bill
import random
#split string method
names_string = input("Give me everybody's names,seperated by a comma.\n")
names = names_string.split(",")
#Get the total number of items in list
num_items = len(names)
#Generate random number between 0 and the last index
random_choice = random.randint(0,num_items-1)
person_who_will_pay = names[random_choice]
or
person_who_will_pay = random.choice(names)
print(person_who_will_pay + " is going to buy the meal today!")
or
print(f"{names[random_choice]} is going to buy the meal today!")
#IndexErrors and Working with Nested Lists
#IndexErrors:list index out of range
num_of_states = len(states_of_america)
print(states_of_america[num_of_states - 1])
#Nested Lists
fruits = ["straberries","Apples","Grapes","Peaches","Cherries"]
vegetables =["Spinash","Kale","Tomatoes","potatoes"]
dirty_dozen = [fruits,vegetables]
print(dirty_dozen)
#Treasure Map
row1 = ["❤","❤","❤",]
row2 = ["❤","❤","❤",]
row3 = ["❤","❤","❤",]
map = [row1,row2,row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure?column and row:\n")
column = int(position[0])
row = int(position[1])
selected_row = map[row - 1]
selected_row[column - 1] = "X"
or
map[row-1][column-1] = "X"
print(f"{row1}\n{row2}\n{row3}")
#Project_Rock Paper Scissors
#print里,若是string,需加双引号,variables 不需要
#在编写代码时,需要debug,考虑出现的各种情况,防止编写完后debug发现出现各种问题
import random
rock ='''
_
| |
_ __ ___ ___| | __
| '__/ _ \ / __| |/ /
| | | (_) | (__| <
|_| \___/ \___|_|\_\
'''
paper ='''
_ __ __ _ _ __ ___ _ __
| '_ \ / _` | '_ \ / _ \ '__|
| |_) | (_| | |_) | __/ |
| .__/ \__,_| .__/ \___|_|
| | | |
|_| |_|
'''
sissors ='''
___ _ _ __ __ _ ___ _ __
/ __| | '_ \ / _` |/ _ \ '__|
\__ \ | | | | (_| | __/ |
|___/_|_| |_|\__, |\___|_|
__/ |
|___/
'''
#games_images = ["rock","paper","sissors"]与下行输出不同
games_images = [rock,paper,sissors]
user_choice =int(input("What do you choose? Type 0 for rock,1 for Paper or 2 for Sissors.\n"))
if user_choice >= 3 or user_choice <0:
print("You typed an invalid number,you lose!")
else:
print(games_images[user_choice])
computer_choice = random.randint(0,2)
print("Computer chose:")
print(games_images[computer_choice])
if user_choice == 0 and computer_choice == 2:
print("You win!")
elif user_choice == 1 and computer_choice == 0:
print("You win!")
elif user_choice == 2 and computer_choice == 0:
print("You lose!")
elif user_choice >computer_choice:
print("You win!")
elif user_choice <computer_choice:
print("You lose!")
elif user_choice == computer_choice:
print("It's a draw")
Day4_Udemy Python 100days
最新推荐文章于 2024-08-19 14:41:29 发布