英文:
How do I make python show the entire equation and the results and not just the results only?
问题
我正在为我的计算机科学项目制作一种骰子滚动器,在这个项目中,我必须询问用户他们想要掷的骰子数量(最多6个)和骰子的面数(无限)。我一直在尝试,但我的输出结果与老师的示例不同。有人可以帮我吗!!这是老师的答案:
我一直在尝试获得与老师相同的输出,但我的答案似乎有点奇怪。看起来我的代码不让Python显示整个等式,例如(3+3+5=11),而是只显示11。有人有解决办法吗?
英文:
I am making sort of a dice roller for my computer science project where I have to ask the user for the number of dices they want to roll(max 6) and the number of sides the dices have(infinity). I've been trying but my output is not the one the teacher sampled. Can anyone help me out!! Here is the teacher's answer:
I've been trying to get the same output as the teacher but my answer comes out a little weird. It seems that my code isn't letting python show the entire equation for example (3+3+5=11) but instead it just shows 11. Does anyone have a solution?
答案1
得分: 1
代码部分不需要翻译,以下是翻译好的部分:
"range" 变量的数据类型是整数,所以你首先必须将它们转换为字符串,然后连接这些变量,像这样:
import random
range1 = random.randint(1, 10)
range2 = random.randint(1, 10)
range3 = random.randint(1, 10)
res = range1 + range2 + range3
print(str(range1) + " + " + str(range2) + " + " + str(range3) + " = " + str(res))
示例输出:
8 + 10 + 9 = 27
查看如何将整数转换为字符串的更多信息,请访问这里:链接
英文:
The data type of range variables are integer so you have to first convert them into string and concate the variables like this
import random
range1=random.randint(1,10)
range2=random.randint(1,10)
range3=random.randint(1,10)
res=range1+range2+range3
print(str(range1)+" + "+str(range2)+" + "+str(range3)+" = "+str(res))
> Example Output:
> 8 + 10 + 9 = 27
check out more on how to convert integer to string here https://www.digitalocean.com/community/tutorials/python-concatenate-string-and-int
答案2
得分: 0
可以将掷骰子的结果存储在一个列表中,然后在结果之间使用加号进行打印(例如,将它们映射为str
并使用join
)。此外,你可以使用for
循环来获取掷骰子的结果,而不是使用那些if
条件。
示例代码:
from random import randint
n = int(input())
k = int(input())
dices = []
for _ in range(n):
dices.append(randint(1, k))
print(' + '.join(map(str, dices)), '=', sum(dices))
结果:
3
6
6 + 1 + 5 = 12
英文:
You can store rolled dice values in a list and then print those values with plus signs in between (for example map them to str
and use join
). Also you could use for
loop for getting the results of the roll instead of those if
s.
Example code:
from random import randint
n = int(input())
k = int(input())
dices = []
for _ in range(n):
dices.append(randint(1, k))
print(' + '.join(map(str, dices)), '=', sum(dices))
Result:
3
6
6 + 1 + 5 = 12
答案3
得分: 0
如果你写 print(2+3)
, 你将得到 5
, 所以你需要写一个包含 '2', '+', 和 '3' 的字符串,例如:
a = 2
b = 3
print(a+b) # 5
print(a, '+', b) # 2 + 3
# 更好的方式是:
print(f'{a} + {b} = {a+b}') # 2 + 3 = 5
英文:
If you write print(2+3)
, you will just have 5
, so you need to write a string with '2', '+' and '3' for example :
a = 2
b = 3
print(a+b) # 5
print(a, '+', b) # 2 + 3
# even better :
print(f'{a} + {b} = {a+b}') # 2 + 3 = 5
答案4
得分: 0
如果你不想处理数组,你可以使用 print("字符串", end="")
。这个参数的值是 '\n',表示换行字符。所以我们可以用任何字符来结束打印语句。
这里我使用空字符串,所以它会是空的。
from random import randint
diceCount = int(input("你要掷多少个骰子(最多6个)"))
sideCount = int(input("每个骰子有多少面?"))
总和 = 0
for i in range(diceCount):
随机数 = randint(1, sideCount)
总和 = 总和 + 随机数
if i == 0:
print(f"{str(随机数)} ",end= '')
else:
print(f" + {str(随机数)}",end='')
print(f" = {总和}")
英文:
if you dont want to deal with arrays you can use print("string", end="")
.The value of this parameter is ‘\n’, the new line character. So we can end the print statement with any character.
Here i give empty string so it is going to be empty
from random import randint
diceCount = int(input("How many dice are you rolling (max 6)"))
sideCount = int(input("How many sides do the dice have?"))
sum = 0
for i in range(diceCount):
randomNum = randint(1, sideCount)
sum = sum + randomNum
if i == 0:
print(f"{str(randomNum)} ",end= '')
else:
print(f" + {str(randomNum)}",end='')
print(f" = {sum}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论