0

该程序的目标是让两个小于 12 的随机数相乘并让用户猜测答案。到目前为止我有这个。. .

import random

g=0
while g<10:
    variable_1 = random.randint (0,13)
    variable_2 = random.randint (0,13)

    answer = variable_1 * variable_2
    guess = input("What is 'variable_1' x 'variable_2'?")

    if guess == answer:
        print "Correct!"
    else:
        print "Incorrect!"

问题是输入框字面意思是“什么是 Variable_1 x Variable_2?”。但是,我希望它在输入框中具有变量的值。有没有办法做到这一点?

4

3 回答 3

6

试试这个:

guess = input("What is %d x %d?" % (variable_1, variable_2))
于 2012-06-08T16:35:52.923 回答
1
querystr="What is "+str(variable_1)+" x "+str(variable_2)+"?";

那么你也能

guess=input(querystr);
于 2012-06-08T16:29:35.527 回答
1
from random import randint

def val(lo=1, hi=12):
    return randint(lo, hi)

def main():
    right = 0
    reps = 10
    for rep in range(reps):
        v1, v2 = val(), val()
        target = v1 * v2

        guess = int(raw_input("What is {} * {}?".format(v1, v2)))

        if guess==target:
            print("Very good!")
            right += 1
        else:
            print("Sorry - it was {}".format(target))

    print("You got {} / {} correct.".format(right, reps))
于 2012-06-08T16:49:47.433 回答