私信  •  关注

Antoine Delia

Antoine Delia 最近创建的主题
Antoine Delia 最近回复了
3 年前
回复了 Antoine Delia 创建的主题 » 如何在Python中缩写这个条件?[重复]

正如@Sayse提到的,你应该使用 all 而不是 any

words_to_check = ["cat", "dog", "rat"]

str1 = "cat, dog, rat"

if all(word in str1 for word in words_to_check):
    print("All words are present")
else:
    print("Not all words are present")

产出:

All words are present
3 年前
回复了 Antoine Delia 创建的主题 » 如何在python的掷骰子程序中打印单个骰子的结果?

我觉得你把事情复杂化了。

一旦你得到了要掷的骰子数和边数,简单地使用for循环就足够了。

import random

r = int(input("Enter the number of dice to roll: "))
s = int(input("Enter the number of sides per die: "))

total = 0
for num in range(r):
    die = random.randint(1, s)
    print(f"Die {num + 1}: {die}")
    total += die

print(f"Total of all dice: {total}")

输出

Enter the number of dice to roll: 2
Enter the number of sides per die: 20
Die 1: 7
Die 2: 2
Total of all dice: 9