简体   繁体   English

"结合输入和随机数的“if”语句问题"

[英]Issue with "if" statement in combination with input and random numbers

I started today playing a little bit with python, so I'm really new.我今天开始玩一点python,所以我真的很新。

I wanted to make a quiz for my little brother, that will generate multiplication functions of random numbers between 0 and 10. Everything went fine, except one thing- when I run the code it always returns false, even when I write a correct input.我想为我的弟弟做一个测验,它将生成 0 到 10 之间随机数的乘法函数。一切都很好,除了一件事——当我运行代码时它总是返回 false,即使我写了一个正确的输入。

I think the main problem is in the "if" statment, and I think the problem is that my input is not really equal to the "multiplication", but I dont know how to fix it.我认为主要问题在于“if”语句,我认为问题在于我的输入并不真正等于“乘法”,但我不知道如何解决它。

I can't understand why the "one" is not equal to the "multiplication", I though the "multiplication" is basically the answer to the multiplication of the two random numbers, and if my input is correct it should be equal and print "true".我不明白为什么“一”不等于“乘法”,我虽然“乘法”基本上是两个随机数相乘的答案,如果我的输入正确,它应该相等并打印“真的”。

I would really like to help, I've been breaking my head over it for couple of hours now.我真的很想帮忙,我已经为此烦恼了几个小时。 Thanks!谢谢!

import random
import sys
import os

random_num1 = random.randrange(0, 10)
random_num2 = random.randrange(0, 10)

print(random_num1, 'X', random_num2, '=', )

def multiplication(random_num1, random_num2):
    sumNum = random_num1 * random_num2
    return sumNum

one = input()

if(one == multiplication(random_num1, random_num2)):
    print('true')
else:
    print('false')

I don't know if you'd still need a 5 year late answer haha.我不知道你是否还需要一个迟到 5 年的答案哈哈。 However, here it is:但是,这里是:

As I was looking at your code, I decided to remove the multiplication function, and just leave it as a normal action, because it isn't really needed at the moment.在查看您的代码时,我决定删除乘法函数,并将其保留为正常操作,因为目前并不真正需要它。 I also removed the other two imports, which weren't being used in the code.我还删除了其他两个未在代码中使用的导入。

Now, the real issue, was that the variable "one" was probably an integer, while the "sumNum" was probably a boolean.现在,真正的问题是变量“one”可能是一个整数,而“sumNum”可能是一个布尔值。 So, this is what I did, so it could compare two values of the same type:所以,这就是我所做的,所以它可以比较相同类型的两个值:

if int(one) == int(sumNum):
    print('true')
else:
    print('false')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM