(Python) - Typing out sum in answer area - python

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.

Related

Complex sequense of number generation provides an unexpected product

I'm making a project where I need to generate a random number (which is later modified). Currently I have this piece of code:
x=input("x = ")
num1_child = randint(0,100)
num1.extend(str(num1_child))
num2 = randint(0,100)
if int(num1[n]) - num2 >= x:
print(str(int(num1) - num2))
print("yes \n")
num_yes += 1
Yes()
elif int(num1) - num2 <= -x:
print(str(num2 - int(num1)))
print("yes \n")
num_yes += 1
Yes()
else:
print(str(int(num1[n]) - num2) + " // " + str(num2 - int(num1)))
print("no \n")
num_no += 1
No()
I want to generate a number between 1 and 100, but it only generates a number netween 50 and 100.
just randint(0, 100) will not work in this usecase, it ahs to be set up using the num1_child etc.
Its quite simple to generate random number-
Simplest way to make a float number between a range:
import random
def makerandom(min,max):
num = random.random()
num = num * (max - min) + min
return num
print(makerandom(0,100))
Get a random integer instead:
import random
print(random.randint(0,100))
There is no random number set that follows a "specific set of rules". A series of numbers can either be (pseudo) random or it can fullfill other specifications. You cannot combine both as per any "rule" you will reduce the entropy.

Python Factorial Program - Printing the Equation

I am writing a program that calculates the factorial of a number, I am able to display the correct answer, however along with the answer I need the actual calculation to display, I am having trouble with that. So for example, when the user enters 4, I need it to display as:
I have been trying to figure out the right code, but do not know what to do.
Here is the code I have so far
number = int(input("Enter a number to take the factorial of: "))
factorial = 1
for i in range(1, number + 1):
factorial = factorial * i
print (factorial)
Right now, it displays the correct answer, however I need for it to include the equation as well as follows: 4! = 1 x 2 x 3 x 4 = 24
The simplest approach is to construct the string as you are iterating:
equation = str(number) + "! = "
factorial = 1
for i in range(1, number + 1):
factorial = factorial * i
equation += str(i) + "x"
equation = equation[:-1] + " = " + str(factorial)
print(equation)
Note that this method appends an unwanted 'x' after the last factor. This is removed by equation[:-1].
Alternatively, you could append this one-line solution to the end of your code. It uses the join method of the string class to concatenate an array of strings:
print(str(number) + "! = " + "x".join(str(n) for n in range(1, number + 1)) + " = " + str(factorial))
As you loop through the numbers to be multiplied, you can append each number's character to a string containing the equation, e.g ans, and print it at last. At the end of the code, I omitted the last letter because I didn't want an extra 'x' to be displayed.
def fact(number):
num_string=str(number)
factorial = 1
ans=num_string+"!="
for i in range(1, number + 1):
factorial = factorial * i
ans+=str(i)+"x"
ans=ans[:-1]
print(ans)
return factorial
fact(4)
You can append each value to the list and then print the equation using the f-string:
num = 5
l = []
f = 1
for i in range(1, num + 1):
f *= i
l.append(i)
print(f"{num}! = {' x '.join(map(str, l))} = {f}")
# 5! = 1 x 2 x 3 x 4 x 5 = 120

Nested Triangle in Python

My assingment
At each level the complete triangle for the previous level is placed into an extra outer triangle. The user should be asked to input the two characters to be used and the width of the innermost triangle, which must be odd. In addition to the test for negative input the function should test whether the supplied number is odd and display an appropriate message if it is not.
I need to print 3 triangles but every one of them includes other. It needs to get printed with two different character(like *-) and the user have to specify the length of innermost triangle and which has to be an odd number. Example,
Example output for 5 value
Ok, let me explain my way,
Every triangle should be in dictionary.
tri1 = {1:"*****", 2:"***", 3:"*"}
tri2 = {1:"..........", ...}
And couldn't find how I can deal with user input?
If enter 5,
length - 5 unit, height 3 unit
length - 11 unit, height 6 unit
length - 23 unit, height 12 unit.
How can i know? What is the logic?
Ok lets say if I did. I understand I should put a tringle in another triangle with nested loop, can simply iterate it another dictionary but, I need to check second character's position.
Thanks in advance.
My code,
ch1, ch2 = input("Please enter the characters you want to use: ")
num = int(input("Please specify the length of innermost triangle(only odd number): "))
if (num % 2 == 0) or (num < 3):
print("Number can not be even, less then 3 and negative")
num2 = (2 * num) + 1
num3 = (2 * num2) +1
tri1 = {}
tri2 = {}
tri3 = {}
for i in range(3):
tri1[i] = ch1*num
num -= 2
check = 1
cont = 0
var = 1
for ii in range(6):
tri2[ii] = ch2*check
check += 2
if (ii >= 3):
tri2[ii] = ch2*var + tri1[cont] + ch2*var
cont += 1
var += 2
for i in tri1:
print('{:^5}'.format(tri1[i]))
for i in tri2:
print('{:^11}'.format(tri2[i]))
The dictionary can be created using a simple function:
def create_tri_dict(tri_chars, tri_height):
level_str = {0:tri_chars[0]}
for i in range(1,tri_height):
level_length = i *2 +1
tri_char = tri_chars[i%2]
level_str[i] = level_str[i-1] + '\n' + tri_char * level_length
return level_str
Then the main logic of your program could be:
tri_chars = input('Input triangle characters: ')
tri_length = int(input('Input triangle base length: '))
tri_height = (tri_length + 1)//2
if tri_length %2 == 0:
raise Exception('Triangle base length not odd')
tri_dict = create_tri_dict(tri_chars, tri_length)
Then to print the final 3(?) triangles:
print(tri_dict[tri_height-2])
print(tri_dict[tri_height-1])
print(tri_dict[tri_height])

How to do factorial using multiplication based on another function that has two parameters

How can I do factorial based on the multiply function? Thats what I have and the problem I'm running into is it gives me 0 instead of 5 != 120.
EDIT NEWEEST: how can I fix the double factorial to give me the right number?
if I do 5, it should give me 5!!= 15 but it gives me 12 why is that?
SOURCE CODE
def multiply(num1,num2):
sum_of_multiplication= 0
for i in range(num2):
sum_of_multiplication = add(sum_of_multiplication,num1)
return sum_of_multiplication
def factorial(num1):
factorial_num = num1
for i in range(1,num1):
num1 = multiply(factorial_num,num1)
print(factorial_num)
print(str(num1) + "!= " + str(factorial_num))
def double_factorial(num1):
double_factorial_num = 2
for i in range(1,num1-2):
double_factorial_num = multiply(double_factorial_num, i)
print(double_factorial_num)
print(str(num1) + "!!= " + str(double_factorial_num))
double_factorial = double_factorial(int(input("please enter your intger:")))
It is because you are including 0 in your multiplication. In the range function (in the factorial function), put 1 as the first parameter to make it look like range(1,num1). This way the multiplication will start at 1, not 0.
Your factorial() is slightly off due to a range() issue, which is an easy mistake to make but you should be able to fix it quickly by reviewing the documentation for range(). However, your double_factorial() function has no hope of success as you fail to deal with a key issue up front, the parity (odd or even) of the number:
def add(a, b):
return a + b
def multiply(a, b):
my_sum = 0
for _ in range(b):
my_sum = add(my_sum, a)
return my_sum
def factorial(number):
product = 1
for multiplicand in range(2, number + 1):
product = multiply(product, multiplicand)
print(str(number) + "!", "=", product)
def double_factorial(number):
parity = number % 2 + 2 # start at 2 or 3
product = 1
for multiplicand in range(parity, number + 1, 2):
product = multiply(product, multiplicand)
print(str(number) + "!!", "=", product)
number = int(input("please enter your integer: "))
factorial(number)
double_factorial(number)
OUTPUT
> python3 test.py
please enter your integer: 5
5! = 120
5!! = 15
>

I am new to python programming and I have an assignment in which I have to print a certain pattern of stars using a function

The pattern is given below. The number of rows is to be specified by the user. In this image it is 6. I know how to print the upper half but I am finding difficulty in the lower half. Please help.
I tried this code:
def asterisk_triangle(n):
x = 1
while (x <= n):
print("*" * x)
x = x + 1
return
If you have top half done, the bottom is similar, but reversed (rather than printing a number of stars equal to the row number, print a number of starts equal to the total number of rows minus the current row number). For example:
num = raw_input("Please enter number: ")
for i in range(num):
print "*" * i
then the opposite would be:
for j in range(num):
print "*" * (num-i)
def pattern(lines):
for i in range(0, lines / 2):
print "*" * i
for i in range(lines / 2, 0, -1):
print "*" * i

Categories