Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_List_Dictionary_Dice - Fatal编程技术网

Python 掷骰子游戏-储存掷骰子结果的列表

Python 掷骰子游戏-储存掷骰子结果的列表,python,python-3.x,list,dictionary,dice,Python,Python 3.x,List,Dictionary,Dice,我正在做掷骰子游戏,在两个骰子显示相同值之前,它会打印出尝试次数(n)。 我还想打印出过去的滚动结果。但是,我的代码只显示最后的滚动结果(n-1次尝试) 我曾尝试用谷歌搜索并查看stackoverflow掷骰子查询的历史记录,但我仍然不知道如何解决代码。请帮忙,我想这和嵌套列表或字典有关,但我就是搞不懂 下面是我的代码: from random import randint stop = 0 count = 0 record = [] while stop == 0: roll =

我正在做掷骰子游戏,在两个骰子显示相同值之前,它会打印出尝试次数(n)。 我还想打印出过去的滚动结果。但是,我的代码只显示最后的滚动结果(n-1次尝试)

我曾尝试用谷歌搜索并查看stackoverflow掷骰子查询的历史记录,但我仍然不知道如何解决代码。请帮忙,我想这和嵌套列表或字典有关,但我就是搞不懂

下面是我的代码:

from random import randint

stop = 0
count = 0
record = []

while stop == 0:
    roll = [(dice1, dice2) for i in range(count)]
    dice1 = randint(1,6)
    dice2 = randint(1,6)
    if dice1 != dice2:
        count += 1
    else:
        stop += 1
    
record.append(roll)

if count == 0 and stop == 1:
    print("You roll same number for both dice at first try!")
else:
    print(f"You roll same number for both dice after {count+1} attempts.") 

print("The past record of dice rolling as below: ")
print(record)

您的代码中有一些错误。首先,我不完全确定

roll = [(dice1, dice2) for i in range(count)]
这条线对你有好处

不过,您可以做一些简单的更改

首先-您的
记录。append(…)
行在循环之外。这就是为什么您只看到上一次运行。只录了一次

其次,您的
while
语句可以是一个简单的
while True:
语句,当您满足匹配条件时,该语句中包含一个
中断。您不需要
stop
变量

from random import randint

count = 0
record = []

while True:
    dice1 = randint(1,6)
    dice2 = randint(1,6)
    record.append([dice1,dice2])
    if dice1 != dice2:
        count += 1
    else:
        break


if count == 0:
    print("You roll same number for both dice at first try!")
else:
    print(f"You roll same number for both dice after {count+1} attempts.")

print("The past record of dice rolling as below: ")
print(record)
输出与此类似:

You roll same number for both dice after 8 attempts.
The past record of dice rolling as below: 
[[1, 6], [2, 1], [1, 6], [5, 6], [5, 3], [6, 3], [6, 5], [4, 4]]

请注意,我已经将
.append(…)
引入了while循环。正如我所描述的,我还对
stop
变量进行了更改。

您的代码中有一些错误。首先,我不完全确定

roll = [(dice1, dice2) for i in range(count)]
这条线对你有好处

不过,您可以做一些简单的更改

首先-您的
记录。append(…)
行在循环之外。这就是为什么您只看到上一次运行。只录了一次

其次,您的
while
语句可以是一个简单的
while True:
语句,当您满足匹配条件时,该语句中包含一个
中断。您不需要
stop
变量

from random import randint

count = 0
record = []

while True:
    dice1 = randint(1,6)
    dice2 = randint(1,6)
    record.append([dice1,dice2])
    if dice1 != dice2:
        count += 1
    else:
        break


if count == 0:
    print("You roll same number for both dice at first try!")
else:
    print(f"You roll same number for both dice after {count+1} attempts.")

print("The past record of dice rolling as below: ")
print(record)
输出与此类似:

You roll same number for both dice after 8 attempts.
The past record of dice rolling as below: 
[[1, 6], [2, 1], [1, 6], [5, 6], [5, 3], [6, 3], [6, 5], [4, 4]]

请注意,我已经将
.append(…)
引入了while循环。正如我所描述的,我还对
stop
变量进行了更改。

我会做一些类似于@telenob的事情。只需使用
而为True:
并在满足条件时中断

以下是我的返工:

from random import randint

roll = 0
die1_record = []
die2_record = []

while True:
    die1 = randint(1,6)
    die2 = randint(1,6)
    die1_record.append(die1)
    die2_record.append(die2)
    
    roll += 1
    if die1 == die2:
        break

print(f"You rolled same number for both dice after {roll} attempt(s).") 
print("The past record of dice rolling is: ")
print(f"die1: {die1_record}")
print(f"die2: {die2_record}")

我会做一些类似于@TeleNoob的事情。只需使用
而为True:
并在满足条件时中断

以下是我的返工:

from random import randint

roll = 0
die1_record = []
die2_record = []

while True:
    die1 = randint(1,6)
    die2 = randint(1,6)
    die1_record.append(die1)
    die2_record.append(die2)
    
    roll += 1
    if die1 == die2:
        break

print(f"You rolled same number for both dice after {roll} attempt(s).") 
print("The past record of dice rolling is: ")
print(f"die1: {die1_record}")
print(f"die2: {die2_record}")

非常感谢您在编码改进方面提供的解决方案和建议。祝你有愉快的一天。非常感谢你的解决方案和关于编码改进的建议。祝您有个美好的一天。