2014-11-21 68 views
0

我對python編碼相當陌生,當我嘗試運行我的python腳本時出現此錯誤,任何人都可以告訴我我在這裏做錯了什麼? 我想做一個數學競賽程序,它應該首先要求兩個球員的名字,然後繼續給每個球員一個問題,直到雙方球員都回答了10個問題。之後,它應該顯示每個玩家得分,並告訴他們誰是贏家。ZeroDivisionError:除零

## 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 
+0

當你運行你的代碼有什麼問題呢?任何錯誤或錯誤的答案? – 2014-11-21 09:46:07

回答

3
num2 = random.randrange(35) 

可以給你零,並會在此行導致除以零:

ans2 = num1/num2 

你可能想是這樣的:

random.randrange(start = 1, stop = 35 + 1) 

這將產生數介於1和35之間(包括兩端)。


A面一句話:除非你想用戶輸入浮點數如0.8333333333333334(這是很可能不會正好等於在你的程序的計算值)爲師(假設你使用python3 ),最好是爲結果和除數輸入一個值,然後從中計算出紅利。

0

Andre Holzner是正確的。這是基本用法的一些例子:

>>> random.random() # Random float x, 0.0 <= x < 1.0 0.37444887175646646

>>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0 1.1800146073117523

>>> random.randint(1, 10) # Integer from 1 to 10, endpoints included 7

>>> random.randrange(0, 101, 2) # Even integer from 0 to 100 26

>>> random.choice('abcdefghij') # Choose a random element 'c'

>>> items = [1, 2, 3, 4, 5, 6, 7] 
>>> random.shuffle(items) 
>>> items 
[7, 3, 2, 5, 6, 4, 1] 

>>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements [4, 1, 5]

要了解更多關於隨機這裏是Link