How can I fix a bug using modulo (%) in Python? - python

I am making a converting program in Python, and in this case it’s from feet to yards. I have having trouble with a specific line of code that is not showing an error message, yet refuses to actually work.
When I run the program and put the following numbers in the input (3, 2, 4, 1) the Yards should be 8, and the feet should be 0, but it stays on 7 yards, and doesn’t add the 3 extra feet into the yard amount.
firstYard = int(input("Enter the Yards: "))
firstFeet = int(input("Enter the Feet: "))
secondYard = int(input("Enter the Yards: "))
secondFeet = int(input("Enter the Feet: "))
print("Yards: ")
print(int(firstYard + secondYard))
print(" Feet: ")
print(int(firstFeet + secondFeet) % 3)
if ((firstFeet + secondFeet) % 3) > 2:
** firstYard += 1
**
The last line is what I’m having trouble with.

Yards = int(input("Enter the Yards: "))
Feet = int(input("Enter the Feet: "))
Yards_2 = int(input("Enter the Yards: "))
Feet_2 = int(input("Enter the Feet: "))
print("\nYards:",(Yards * 3 + Yards_2 * 3 + Feet + Feet_2) // 3, "Feet:",(Yards * 3 + Yards_2 * 3 + Feet + Feet_2) % 3)
You have to scroll to the right more.
The feet should not exceed 2 because 3 feet would be a yard.

This line is causing your problem:
if ((firstFeet + secondFeet) % 3) > 2:
The remainder of division by three cannot be greater than 2, maybe you mean to see if three fits more than twice? Try division instead of modulo if that's the case.

Your test
if ((firstFeet + secondFeet) % 3) > 2:
will always be false, because you're taking the modulo of that sum and any number module 3 cannot be greater than 2.
What you need to add to your yard total is the integer division of the feet total:
firstYard += (firstFeet + secondFeet) // 3
I would structure your code differently:
totalYard = firstYard + secondYard + (firstFeet + secondFeet) // 3
totalFeet = (firstFeel + secondFeet) % 3

Related

calculator that prints step by step solution in python

my son is trying to make a calculator to help him with the many pages of homework that he gets. I know the simple solution would be to tell him about wolfram alpha, but I thought this could be a fun project. I need some help with how to iterate over the rest of the digits and print the solution in a step-by-step format. This is what he has so far:
# Variables
X = input("input the first number with space between digits: ")
Y = int(input("input the second number: "))
Xn = X.split(" ")
if int(Xn[0]) >= Y: # if Y fits in X the do a simple division and return the remainder
Xy1 = int(Xn[0])
fit0 = int(Xy1 / Y)
rem0 = Xy1 - fit0 * Y
print(" It fits " + str(fit0), "times ", " the remainder is: " + str(rem0))
else: # if Y does not fit in X the add the first two strings of X and convert them to integers then do a simple
# division and return the remainder
Xy0 = (int(Xn[0] + Xn[1]))
fit = int(Xy0 / Y)
rem = Xy0 - fit * Y
print(" It fits " + str(fit), "times ", " the remainder is: " + str(rem))
Here, I made an example that prints step by step the division.
I hardcoded the x (dividend with digits separated by spaces) and the divisor.
You can just change it to incorporate the inputs from the user
x = "1 4 8 7"
divisor = 5
# convert x to a list of integers
x = [int(i) for i in x.split(" ")]
dividend = x[0]
i = 0 # index of the current dividend digit
quotient = ""
while i < len(x)-1:
i += 1
quotient += str(dividend // divisor)
remainder = dividend % divisor
print(f"{dividend} / {divisor} -> {dividend // divisor} (remainder {remainder})")
dividend = remainder * 10 + x[i]
quotient += str(dividend // divisor)
remainder = dividend % divisor
print(f"{dividend} / {divisor} -> {dividend // divisor} (remainder {remainder})")
print(f"Result: {quotient} (remainder {remainder})")
This gives as result:
1 / 5 -> 0 (remainder 1)
14 / 5 -> 2 (remainder 4)
48 / 5 -> 9 (remainder 3)
37 / 5 -> 7 (remainder 2)
Result: 297 (remainder 2)
I think I misunderstood the question... why not use float?
x = float(input("input the first number: "))
y = float(input("input the second number: "))
print(f" It fits {x//y} times , the remainder is: {x/y-x//y}")

(Python) - Typing out sum in answer area

I've been trying to code a system to solve one of those "Are you a bot" type of captchas in a game.
Pretty much what it does is gives you a mathematical question from 1-200 numbers (ex 19 + 163) and asks you to solve it at random times throughout the gameplay.
with Pyautogui I managed to find the numbers accordingly on the screen (scanning the region for the number 1-9, then adding it to the according variable (i made 5 variables from number1 to number5)
Using the example above (19 + 163) it would correspond with
19
number1 = 1, number2 = 9
163
number3 = 1, number4 = 6 and number5 = 3
and then i made a simple calculating system which is :
sum1 = ((number1 * 10) + number2)
sum2 = ((number3 * 100) + (number4 * 10) + number5)
sum = sum1 + sum2
print(sum)
But is there a way to make it so that the sum will display it in 3 seperate numbers instead of showing let's say (sum = 193) it would show (sum = 1, 9, 3) and then type it out in the answer area (I had an idea of using import keyboard for typing the answer out but i'm not sure if it works in this case)
or even better in this case if there is a way to take the sum and then make it type it out in the answer area by the code?
https://imgur.com/a/XYXrgeX (Picture of the Bot captcha)
Try this:
sum1 = ((number1 * 10) + number2)
sum2 = ((number3 * 100) + (number4 * 10) + number5)
sum_ = sum1 + sum2
lst = list(str(sum_))
sum_ = ""
for i in lst:
sum_ = sum_ + str(i) + ", "
print(sum_[:-2])
This basically turns the sum into a list of digits and then combines then all while adding commas in between.

Python Converting picoseconds and femtoseconds not giving the right time in seconds

I'm converting from all units of time to seconds and for some reason with the smaller units of time (picoseconds and femtoseconds), i'm getting (femtoseconds): 0.00000000000000100000000000000007770539987666107923830718560119501514549256171449087560176849365234375 instead of 0.000000000000001. Does anyone know why?
days = int(input("Enter the amount of days: ")) * 24 * 60 * 60
hours = int(input("Enter the amount of hours: ")) * 60 * 60
minutes = int(input("Enter the amount of minutes: ")) * 60
ms = int(input("Enter the amount of milliseconds: ")) * (10 ** -3)
mcs = int(input("Enter the amount of microseconds: ")) * (10 ** -6)
ns = int(input("Enter the amount of nanoseconds: ")) * (10 ** -9)
ps = int(input("Enter the amount of picoseconds: ")) * (10 ** -12)
fs = int(input("Enter the amount of femtoseconds: ")) * (10 ** -15)
s = days + hours + minutes + ms + mcs + ns + ps + fs
print("The amount of seconds is:", "{0:.50}".format(s))
Floating point numbers cannot be accurately represented in any programming language, simply because there is an infinite number of them. However, what might help you is Decimal: Clarification on the Decimal type in Python
Documentation: https://docs.python.org/3.8/library/decimal.html

Program that counts odd numbers and then prints them

So I have question about my homework.
Program must do:
* Asks from user the number of clients ( not negative int number )
* Uses while and gets total number of flowers
* Print final sum to screen.
We have text like:
It's womens day and flowershop decided to give flowers to women. But thing is, only odd number gets them.
So first one gets 1, second one gets nothing, third gets 3 , fifth gets 5 and so on. If you insert 7, then sum of odd numbers is 16 : 1 + 3 + 5 + 7 = 16. If user inserts 8, then sum is also 16 : 1 + 3 + 5 + 7 = 16.
The odd number can't be bigger than number of women.
You have to insert the number of women.
I have done this:
women = int(input("Insert number of buyers: "))
i = 1
sum = 0
while i < women:
i = i + 2
sum = sum + i
print("Total of flowers is: " + str(women))
But it dosent work and my brain is totally out of ideas already :(
Final result must look like this:
Insert number of buyers: 7
Total of flowers is : 16
There are three flaws in your code:
Incrementing i before incrementing sum (meaning the first lady gets 3 flowers)
Using 1 based indices to count the women, but using the wrong loop condition (with women=7 the loop body will not execute if i==7, so it should be i <= women)
Not printing the answer (sum) but the input (women)
Here is a fixed version:
women = int(input("Insert number of buyers: "))
i = 1
sum = 0
while i <= women:
sum = sum + i
i = i + 2
print("Total of flowers is: " + str(sum))
Using a for loop would, to my opinion, be simpler.
women = int(input("Insert number of buyers: "))
sum = 0
for i in range(1,women+1):
if i%2 != 0: # If i is odd
sum += 1
print("Total of flowers is: " + str(sum))
or
women = int(input("Insert number of buyers: "))
sum = sum(i for i in range(1,women+1) if i%2 != 0)
print("Total of flowers is: " + str(sum))
Do this with a list comprehension:
women = int(input("Insert number of buyers: "))
flowers = sum(i for i in range(1, women+1) if i%2 == 1)
print("Total of flowers is:", flowers)
Or using range's step parameter:
women = int(input("Insert number of buyers: "))
flowers = sum(range(1, women+1, 2))
print("Total of flowers is:", flowers)
Alternatively with a loop it could look like this:
women = int(input("Insert number of buyers: "))
flowers = 0
for i in range(1, women+1):
if i%2 == 1:
flowers += i
print("Total of flowers is:", flowers)
Or look like this using a loop and range's step parameter:
women = int(input("Insert number of buyers: "))
flowers = 0
for i in range(1, women+1, 2):
flowers += i
print("Total of flowers is:", flowers)
In future production code, you would go for variant 1 or 2.
Your Simply Revised code:
women = int(input("Insert number of buyers: "));
i = 1;
sum = i;
if women%2==0:
women=women-1;
while i < women:
i = i + 2;
sum = sum + i;
print("Total of flowers is: " + str(sum));

Shoe size converter function

I am trying to make a shoe size converter function. But this program prints weird things like:
"You need function shoe_size at 0x030236F0> sized shoes"
What do I have to do? Here is my code:
def shoe_size(foot_size):
shoe_size = (foot_size + 1,5) * 3 / 2
return shoe_size
foot_size = (input("Enter your foot size: "))
print ("You need " + str(shoe_size) + " sized shoes")
There are a few errors or at least potential errors here check the changes I made to the input statement and you usually don't want a variable that is the same name as the function it is in so:
def shoe_size(given_size):
#foot_size = (foot_size + 1,5) * 3 / 2 #This multiples a tuple (, as .)
return (given_size + 1.5) * 3 / 2 #returning float (1.5 makes float)
foot_size = int(input("Enter your foot size: "))
#figured you wanted a type cast here: used int just change to float if halfs wanted
print ("You need " + str(shoe_size(foot_size)) + " sized shoes")
#this converts and prints the size: Your original was treating the function as a variable
You need to give the foot_size variable to your shoe_size method in your print statement:str(show_size(foot_size))
def shoe_size(foot_size):
shoe_size = (foot_size + 1.5) * 3 / 2
return shoe_size
foot_size = (input("Enter your foot size: "))
print ("You need " + shoe_size(foot_size) + " sized shoes")
This is the corrected script:
def shoe_size(foot_size):
shoe_size = (foot_size + 1.5) * 3 / 2
return shoe_size
foot_size = (input("Enter your foot size: "))
print ("You need " + str(shoe_size(foot_size)) + " sized shoes")

Categories