Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将“游戏”放入游戏中_Python_Python 3.x - Fatal编程技术网

Python 将“游戏”放入游戏中

Python 将“游戏”放入游戏中,python,python-3.x,Python,Python 3.x,我正在用python编写一个简单的骰子游戏,多亏了这里的一些帮助,我已经对它进行了改进,但我的问题是,当我希望实现掷硬币来确定哪个玩家先去时,我不知道从哪里开始 骰子游戏代码: import random (a, b, c, d) = [random.randint(1, 6) for _ in range(4)] a=b=c=d=ta=tb=0 while ta<100 and tb<100: ta+=a+b tb+=c+d

我正在用python编写一个简单的骰子游戏,多亏了这里的一些帮助,我已经对它进行了改进,但我的问题是,当我希望实现掷硬币来确定哪个玩家先去时,我不知道从哪里开始

骰子游戏代码:

    import random
    (a, b, c, d) = [random.randint(1, 6) for _ in range(4)]
    a=b=c=d=ta=tb=0
    while ta<100 and tb<100: 
      ta+=a+b
      tb+=c+d 
      (a, b, c, d) = [random.randint(1, 6) for _ in range(4)]
      print("Vai vēlaties mest?")
      mest=input("Y/N==>")
      if mest == "y" or mest=="Y":   
       print("Jūs uzmetāt:")
       print (a,"un",b,"|", a+b+ta,)

       print("Pretinieks uzmeta:")
       print (c,"un",d,"|", c+d+tb)

      elif mest=="n" or mest=="N":
        print ("Jūs zaudējāt")
        print("Jūsu punktu skaits:",ta)
        print("Pretinieka punktu skaits:",tb)
        break
    if ta>=100 or tb>=100:
     print("Spēle beigusies")
     if ta>tb:
       print("Jūs uzvarējāt!")
       print("Jūsu punktu skaits:",ta)
       print("Pretinieka punktu skaits:",tb)
     elif tb>ta:
       print ("Jūs zaudējāt!")
       print("Jūsu punktu skaits:",ta)
       print("Pretinieka punktu skaits:",tb)
总而言之,我希望实现的是这两种代码的结合。我希望程序从第一个代码开始,如果猜测正确,骰子游戏将首先显示a+b+ta的结果,如果猜测不正确,它将在游戏的其余部分首先显示c+d+tb的结果。 另外,为了使它更方便,我还有一个关于第二个代码的问题,你如何从选项中为一个单词指定一个定义字母,例如,如果我输入m,它将识别它为moneta等等。 附笔
很抱歉问了这么长的问题和糟糕的形式,我对这件事还是新手,我想尽可能多地学习

我不确定你的代码到底应该如何工作,但我想你应该这样做:首先我洗牌一个球员名单,所以player1要么是人类球员,要么是计算机。然后,如果第一个播放器=='human'或=='computer',您就可以检入代码的其余部分

在我的示例中,我只是让用户重新掷骰子,如果他是第一个玩家,则更改变量a,如果他是第二个玩家,则更改变量b

import random


players = ['human', 'computer']
random.shuffle(players)
# Assign the shuffled player names to these two variables.
player1, player2 = players

a = random.randint(1, 6)
b = random.randint(1, 6)

# Then print the dice of the players.
print(f'{player1} rolled {a} and {player2} rolled {b}')

user_input = input('Enter "y" to roll again: ')

# Just check if the input starts with 'y'.
if user_input.lower().startswith('y'):
    # Change either variable a or b depending on the first player.
    if player1 == 'human':
        a = random.randint(1, 6)
    else:
        b = random.randint(1, 6)

print(f'{player1} rolled {a} and {player2} rolled {b}')

你是在问如何在while循环中的两个玩家之间循环,并从一个随机玩家开始?例如,玩家2开始,他做出选择,然后游戏要求玩家1做出选择,依此类推。因此,代码的基本jist是,玩家在计算机上玩骰子。唯一的选择是由玩家做出的。第一选择t.i投币如何决定谁的值将首先显示,如果选择==投币,玩家的点值将首先显示,如果不是,第一个值将在游戏的其余部分显示在计算机上。听起来你可以简单地生成一个介于0和1之间的随机数-如果0,第一个玩家将首先显示,如果1,第二个播放器首先运行。如果您使用的是比3.6更旧的Python版本,则必须使用str.format方法而不是f-string:print“{}rolled{}和{}rolled{}”。formatplayer1,a,player2,b。谢谢,我能够组合这两个代码,通过简单地将抛硬币代码放在循环外,并将if choice==硬币与一组打印输出一起添加到循环中,或者与其他打印输出一起添加到循环中。现在我只需要找到一种方法来添加另一个循环,它将提供在达到点值后再次播放的选项太棒了!为了让用户再次玩,我将游戏代码放入一个函数中,然后在另一个外部while循环中调用它。游戏结束后,只需询问用户是否想再次玩,如果输入“否”,则中断while循环退出。
import random


players = ['human', 'computer']
random.shuffle(players)
# Assign the shuffled player names to these two variables.
player1, player2 = players

a = random.randint(1, 6)
b = random.randint(1, 6)

# Then print the dice of the players.
print(f'{player1} rolled {a} and {player2} rolled {b}')

user_input = input('Enter "y" to roll again: ')

# Just check if the input starts with 'y'.
if user_input.lower().startswith('y'):
    # Change either variable a or b depending on the first player.
    if player1 == 'human':
        a = random.randint(1, 6)
    else:
        b = random.randint(1, 6)

print(f'{player1} rolled {a} and {player2} rolled {b}')