Python ZeroDivision错误:被零除

Python ZeroDivision错误:被零除,python,Python,我对python编码相当陌生,当我尝试运行python脚本时,我遇到了这个错误,有人能告诉我我在这里做错了什么吗? 我正在尝试制作一个数学竞赛程序,它应该首先询问两位选手的名字,然后继续给每位选手一个问题,直到两位选手都回答了10个问题。之后,它应该显示每个玩家的得分,并告诉他们谁是赢家 ## Maths Competition ## import sys import time import random p1_score = 0 p2_score = 0 main_loop = 'y' l

我对python编码相当陌生,当我尝试运行python脚本时,我遇到了这个错误,有人能告诉我我在这里做错了什么吗? 我正在尝试制作一个数学竞赛程序,它应该首先询问两位选手的名字,然后继续给每位选手一个问题,直到两位选手都回答了10个问题。之后,它应该显示每个玩家的得分,并告诉他们谁是赢家

 ## Maths Competition ##
import sys
import time
import random
p1_score = 0
p2_score = 0
main_loop = 'y'
loop = 'y'
if sys.platform == 'darwin':
    print('Welcome Mac user')
elif sys.plaform == 'win32' or 'win64':
    print('Welcome Windows user')
else:
    print('Welcome Linux user')
time.sleep(2)
print('This is a two player maths competition game, Player 1, please enter your name.')
p1_name = input()
print('now player 2 please..')
p2_name = input()
print('Processing...')
time.sleep(2)
print(p1_name+''' will first be given a random maths question,
they then have to answer that question or just press enter if they can't get it.
Then '''+ p2_name +''' will be given a question and they have to do the same thing. Each
time a player gets an answer correct, 10 points are automatically added to their score.
Each player will be given 10 questions in total, in the end, the one with the most right
answers will win. If it is a draw, a penalty round will happen, enjoy
Ps. '**' means 'to the power off'. ''')
time.sleep(5)
while main_loop == 'y':
    num_of_tries = 0
    while loop == 'y':
     num_of_tries = num_of_tries + 1
     if num_of_tries >20:
         break
     ops = ['x','/','+','-','**']
     num1 = random.randrange(100)
     num2 = random.randrange(35)
     sel_op = random.choice(ops)
     print(p1_name+', please press enter once you are ready to get your question')
     input()
     if sel_op == 'x':
         ans = num1 * num2
     elif sel_op == '/':
         ans = num1 / num2
     elif sel_op == '+':
         ans = num1 + num2
     elif sel_op == '-':
         ans = num1 - num2
     elif sel_op == '**':
         ans = num1 ** num2
     p1_ans = input('Your question is: %d %s %d' % (num1,sel_op,num2))
     if p1_ans == ans:
         p1_score = p1_score + 10
     num1 = random.randrange(100)
     num2 = random.randrange(35)
     sel_op = random.choice(ops)
     print(p2_name+', please press enter once you are ready to get your question')
     input()
     if sel_op == 'x':
         ans2 = num1 * num2
     elif sel_op == '/':
         ans2 = num1 / num2
     elif sel_op == '+':
         ans2 = num1 + num2
     elif sel_op == '-':
         ans2 = num1 - num2
     elif sel_op == '**':
         ans2 = num1 ** num2
     p2_ans = input('Your question is: %d %s %d' % (num1,sel_op,num2))
     if p2_ans == ans2:
         p2_score = p2_score + 10
    print(p1_name+' got %d' % (p1_score))
    print(p2_name+' got %d' % (p2_score))
    if p1_score > p2_score:
         print(p1_name+' is the WINNER!')
    elif p2_score > p1_score:
         print(p2_name+' is the WINNER!')
    print('Would you like to play another? y/n')
    repeat = input()
    if any ( [repeat == 'y', repeat == 'Y'] ):
         print('Sure thing, wait a couple of seconds for me to set things up again...')
         time.sleep(3)
    elif any ( [repeat == 'n', repeat == 'N'] ):
         break
    else:
         print('I\'ll take that as a NO')
         time.sleep(2)
         break
可以给您零,并将导致在此行中除以零:

 ans2 = num1 / num2
您可能需要以下内容:

 random.randrange(start = 1, stop = 35 + 1)
将生成介于1和35之间的数字(包括1和35)


旁注:除非您希望用户为除法(假设您使用的是python3)输入浮点数,例如0.8333334(很可能不完全等于程序中计算的值),否则最好为结果和除数抛出一个值,然后从中计算红利

可以给您零,并将导致在此行中除以零:

 ans2 = num1 / num2
您可能需要以下内容:

 random.randrange(start = 1, stop = 35 + 1)
将生成介于1和35之间的数字(包括1和35)



旁注:除非您希望用户为除法输入浮点数,例如0.8333334(很可能不完全等于程序中计算的值)(假设您使用的是python3),最好为结果和除数抛出一个值,然后从中计算红利。

安德烈·霍尔兹纳是正确的。以下是一些基本用法的示例:

>>random.random()#random float x,0.0>>random.uniform(1,10)#random float x,1.0>>random.randint(1,10)#1到10的整数,包括端点
7

random.randrange(0,101,2)#0到100之间的偶数整数 26

>random.choice('abcdefghij')#选择一个随机元素
“c”

>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]
>随机。样本([1,2,3,4,5],3)#选择3个元素
[4,1,5]


要了解有关random的更多信息,请点击这里的

安德烈·霍尔兹纳是正确的。以下是一些基本用法的示例:

>>random.random()#random float x,0.0>>random.uniform(1,10)#random float x,1.0>>random.randint(1,10)#1到10的整数,包括端点
7

random.randrange(0,101,2)#0到100之间的偶数整数 26

>random.choice('abcdefghij')#选择一个随机元素
“c”

>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]
>随机。样本([1,2,3,4,5],3)#选择3个元素
[4,1,5]


要了解有关random的更多信息,请参见

运行代码时出现了什么问题?回答有错误吗?!运行代码时出现了什么问题?回答有错误吗?!