0

このコードを実行すると、2 番目の定義で問題が発生します。* de *f 乗算 (): 構文エラーを受け取ると、def の de が歌われます。

import random
def start () :

    print "Welcome!"
    choose ()

def choose () :

    choice = input """would you like to
     add, subtract, or multiply?
      1       2            3
    """
    if choice = 1 :
        add ()
    if choice = 2 :
        subtract ()
    if choice = 3 :
        multiply ()


def multiply () :

    x = random.random ()
    x = round ()
    y = random.random ()
    y = round ()
    print "What is the answer to: ", x,"*", y, " ?"
    answer = input ": "
    z = x*y
    if answer == z :
        print "you are correct!"
    elif answer < z :
        print "your answer is low! The correct answer was ", z
    elif answer > z :
        print "your answer is high! The correct answer was ", z
    multiply ()

def add () :

    x = random.random ()
    x = round ()
    y = random.random ()
    y = round ()
    print "What is the answer to: ", x,"+", y, " ?"
    answer = input ": "
    z = x+y
    if answer == z :
        print "you are correct!"
    elif answer < z :
        print "your answer is low! The correct answer was ", z
    elif answer > z :
        print "your answer is high! The correct answer was ", z

def subtract () :

    x = random.random ()
    x = round ()
    y = random.random ()
    y = round ()
    print "What is the answer to: ", x,"*", y, " ?"
    answer = input ": "
    z = x*y
    if answer == z :
        print "you are correct!"
    elif answer < z :
        print "your answer is low! The correct answer was ", z
    elif answer > z :
        print "your answer is high! The correct answer was ", z
4

2 に答える 2

2

inputは関数なので、次のように呼び出す必要があります。

input('Input some stuff: ')

次のような行もいくつかあります。

if choice = 1 :

あなたは書きたいですchoice == 1。最後に、この部分は少し奇妙です。

x = random.random ()
x = round ()

あなたはおそらくに渡しxたいround

x = random.random ()
x = round (x)

または、その部分を完全にスキップして使用しますrandint

x = random.randint(0, 1)
于 2013-01-24T04:30:20.747 に答える
0

コード内の論理エラーと構文エラーは次のとおりです。

answer = input ": "

次のように入力を呼び出します。

answer = input(": ")


if choice = 1 :

= は代入です。==という意味です。


x = random.random ()
x = round ()

x を最初に代入してから 2 番目に代入すると、最初の代入が起こらなかったようになります。ということx = round(x)ですか?


于 2013-01-24T04:31:52.350 に答える