简体   繁体   中英

how to print true or false depending on addition in python?

I am a bit confused on how to get this to print either true or false, for example, you have 4 of Xobject which have a value of 2 each and you have 5 of Yobject which have a value of 5 each.

you have to see if 3 xobject and 2 yobject fit into 15. this should print false as you are not able to break the objects in half or count all of them together.

as in you can not do 3(2) + 2(5) = 16, which is larger than 15 but involves you adding the objects together which is not allowed as they are meant to be two completely different things

ok so here's one way of doing it:

 x = 2 #value of each x object
 y = 5 #value of each y object

 xObjects = int(input('How many x objects:'))
 yObjects = int(input('How many y objects:'))


 if ((xObjects * x) + (yObjects * y)) == 15:
    print("True")
 else:
    print("False")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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