例子来了然后根据要求写
只用define,while,if. 图一上有例子 ,得有个main procedure,简单一点的program设计就行 像是猜翻硬币就可以了,希望可以快点谢谢 一定要跟图片上要求一样!
写完了,可以试一下:
游戏机制如下:
在游戏中:
1、同样的名字进入游戏,会进入之前的游戏积分。
2、top down left right 可能得0分也可能得高分,随机。
3、没见过的名字,积分为0,新开始游戏。
注意: 选择的时候:'Y', 'N'注意大小写。
import random
def main():
global points, player
points = []
player = []
greet()
return
def greet():
smile_face = "\U0001f600"
print(f'{smile_face} Welcome !It is a game of throwing sieves.')
continue_flag = True
while continue_flag:
name = input("You can start the game. Please input your name:('N' exit the game)")
if name == 'N':
print(f'{smile_face} Exit !')
continue_flag = False
if name in player:
_index = player.index(name)
_score = points[_index]
print(f'Hello {name}, the name already exists and your score is {_score}')
print(f'Please input your choice:')
print(f'1: start')
print(f'2: input your name again')
print(f'Others: exit')
choice = input()
if choice == '1':
score = play(_score)
points[_index] = score
_exit = input("Exit the game:(Y or N)")
if _exit == 'Y':
continue_flag = False
print(f'Exit!')
else:
print(f'Restart!')
continue_flag = True
elif choice == '2':
continue_flag = True
else:
print(f'Exit!')
continue_flag = False
print(f'{smile_face} Exit !')
else:
print(f'Hello {name}, Your score is 0. Start')
player.append(name)
score = play(init_score=0)
points.append(score)
_exit = input("Exit the game:(Y or N)")
if _exit == 'Y':
continue_flag = False
print(f'Exit!')
else:
print(f'Restart!')
continue_flag = True
def play(init_score):
total_score = init_score
print(f'You have {init_score}!')
continue_flag = True
while continue_flag:
print(f'Please input your choice:')
print(f'1: top')
print(f'2: right')
print(f'3: down')
print(f'4: left')
print(f'Others: exit')
_input = input()
random_dict = {
'1': 10 * random.randint(0, 1),
'2': 20 * random.randint(0, 1),
'3': 30 * random.randint(0, 1),
'4': 40 * random.randint(0, 1),
}
if _input in random_dict.keys():
add_score = random_dict[_input]
total_score += add_score
print(f'You have selected the option:{_input}')
print(f'You get the score :{add_score}')
print(f'Your total score is :{total_score}')
else:
continue_flag = False
print(f'Exit')
print(f'Your total score is :{total_score}')
return total_score
if __name__ == "__main__":
main()
测试结果:
/usr/bin/python3.8 /home/hyh/Project/MyTools/1/cyoa.py
😀 Welcome !
You can start the game. Please input your name:('N' exit the game)xiaozi
Hello xiaozi, Your score is 0. Start
You have 0!
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
1
You have selected the option:1
You get the score :10
Your total score is :10
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
3
You have selected the option:3
You get the score :30
Your total score is :40
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
exit
Exit
Your total score is :40
Exit the game:(Y or N)y
Restart!
You can start the game. Please input your name:('N' exit the game)xiaozi
Hello xiaozi, the name already exists and your score is 40
Please input your choice:
1: start
2: input your name again
Others: exit
1
You have 40!
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
2
You have selected the option:2
You get the score :0
Your total score is :40
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
3
You have selected the option:3
You get the score :30
Your total score is :70
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
exit
Exit
Your total score is :70
Exit the game:(Y or N)y
Restart!
You can start the game. Please input your name:('N' exit the game)n
Hello n, Your score is 0. Start
You have 0!
Please input your choice:
1: top
2: right
3: down
4: left
Others: exit
exit
Exit
Your total score is :0
Exit the game:(Y or N)Y
Exit!
Process finished with exit code 0
import random
def func():
guess = ' '
toss = random.randint(0, 1)
while guess not in range(0, 2):
print('Guess the coin toss!Enter heads or tails')
guess = int(input())
# toss=random.randint(0,1)
if guess == toss:
print('You got it')
else:
print('Regretful!Guess again')
guess = int(input())
if guess == toss:
print('You got it')
else:
print('Sorry.You are bad at this game')
if __name__ == '__main__':
func()
如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢
看不懂英文,要不来个中文翻译翻译?
根据题目要求,编写如下代码供参考:
import random
def main():
global points,player
points=0
greet()
while True:
print('start your experience\n1.exp1\n2.exp2\n3.exit\n')
choice = input('choice number:')
if choice=='1':
advanture()
elif choice=='2':
rand_walk()
elif choice == '3':
print(f'{player},you\'ve got {points} experience points')
break
def get_points(dic):
global points
if dic=='n':points+=1
elif dic=='s':points+=2
elif dic=='e':points+=3
else:points+=0
def advanture():
global points, player
dic=input('direction you choice is(nswe):')
get_points(dic)
def rand_walk():
global points, player
dic=random.choice(list('nwse'))
get_points(dic)
def greet():
global points, player
player=input('please input your name:')
print(f'Welcome to the game,{player}')
if __name__=="__main__":
main()