I want to let my script stop, first I ask when I want to stop it:
length = input("how many times " + str(num1) + str(" do you want to see?"))
(E.G. I say ‘3’ the length should be 3)
Then I want to ‘ask’ if length is the same as where it must be when it should finish:
if length == times (times is where my script is ATM)
This doesn’t work, but if I set times to a number it does work…
Even when I print both of them (print (times, length)) it shows the same (3, 3) in my console, but it doesn’t do something… this is my script:
length = input("how many times " + str(num1) + str(" do you want to see?"))
print("starting!")
time.sleep(2)
ORnum1 = num1
times = 1
while CalcMeth == "repeat":
if C == "Yes":
print(times, "times", ORnum1, "=", num1)
num1 = num1 + ORnum1
if length == times:
C = "no"
print("Reached End, stopping.")
else:
time.sleep(num2)
times = times + 1
elif C == "no":
time.sleep(2)
print ("stopped!")
sys.exit()
if length == times:
here in your code length is in str and times is in integer.
length = int(input("how many times " + str(num1) +" do you want to see?))
Related
import random
x = 50
score = 0
number = random.randint(1, x)
[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))
if number < x/2:
print('This number is less than ' + str(int(x/2)))
else:
print('This number is larger than ' + str(int(x/2)))
print(number)
while True:
if int(input('Guess: ')) == number:
print('You got it')
break
else:
print('Try again!')
What the code does so far is takes a random integer between 1 and whatever number I want. It tells me which numbers it is divisible by between 1-9 and also if it is bigger than half the maximum possible number. It essentially gives you a lot of info to guess.
I want to add a score aspect where after you guess the correct number, you will get 1 added to your score. Then it will loop back to the beginning, get a new number to guess and give all it's information again so you can guess. I'm trying to get the looping part but I'm really lost right now.
When your guess is correct we can add 1 to your current score and print it. You can play till you guess it right. You have to put the whole code in a while loop for looping through the game after every correct answer. You can break the loop if your score is greater than 10 and the game stops.
import random
x = 50
score = 0
while True:
if score >= 10:
break
number = random.randint(1, x)
[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))
if number < x/2:
print('This number is less than ' + str(int(x/2)))
else:
print('This number is larger than ' + str(int(x/2)))
print(number)
while True:
if int(input('Guess: ')) == number:
print('You got it')
score+=1
print('Your current score',score)
break
else:
print('Try again!')
Haven't worked with Python myself before but I assume you want to encapsulate everything inside a huge while loop and ask at the end of each iteration if you want to keep playing
Something like this (this is more pseudocode than anything, didn't even tested it)
import random
x = 50
score = 0
keepPlaying = True
while keepPlaying:
number = random.randint(1, x)
[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))
if number < x/2:
print('This number is less than ' + str(int(x/2)))
else:
print('This number is larger than ' + str(int(x/2)))
print(number)
if int(input('Guess: ')) == number:
print('You got it')
score++
break
else:
print('Try again!')
score--
if (input("do want to keep playing?")=="no")
keepPlaying = False
the problem says to print "that's a really really ... big number" with one "really" for every extra digit that the number has (so 15 would be "that's a really big number", 150 would be "that's a really really big number", 1500 would be "that's a really really really big number", and so on.)
the input is an integer, and the only requirements listed are that the code should run correctly with any integer, should use a while loop to keep dividing the number by 10 and should use += to add onto the end of a string
x = input(("input an integer: "))
count = len(x)
y = int(x / 10)
countx = count - 1
print("that's a " + count("really") + " big number")
i don't really know what i did, but i can tell it's not correct
Try this one. Uses while loop to divide number by 10 and += to add onto the string. You will need to take a string variable and then append to it as count increases. Loop will run until number >= 10 as conditions you mentioned.
x = int(input(("input an integer: ")))
str=""
while x>=10:
x=x//10
str+="really "
print("that's a " + str + "big number")
Added the while loop as you stated in your question
strVar = ""
y = 0
length = 0
cnt = 0
finished = False
num = input("Type a number: ")
while not finished:
y = int(num)//10
length = int(len(str(y)))
if length <= cnt:
finished = True
else:
strVar += " really"
cnt += 1
print("That's a" + strVar + " big number!")
Give this a try. It finds how many trailing zeroes there are in a given number and based on that creates a certain amount of really's.
x = input(("input an integer: "))
count = int(len(x) - len(x.rstrip('0')))
if count == 0:count = 1
really = "really "*count
print(f"that's a {really} big number")
Try this
x = int(input("enter an integer"))
print("that's a " + 'really '*(len(str(x))-1) + " big number")
If you want to correct your function use this
x = input(("input an integer: "))
count = len(x)
y = int(x) / 10
countx = count - 1
print("that's a " + countx*" really" + " big number")
When I choose question 3, even I do put in the right answer(value_c * value_d, it tells me that it is incorrect. Help?
import random
value_a = random.randint(100, 999)
value_b = random.randint(100, 999)
value_c = random.randint(21, 89)
value_d = random.randint(21, 89)
value_e = random.randint(81, 100)
value_f = random.randint(81, 100)
print('''Straight to the point. Pick an option.
1. 2 numbers with 3-digit values added.")
2. 2 numbers with 3-digit values added and then multiplied by 2.
3. 2 numbers with 2-digit values and less than 89 multiplied.
4. 2 numbers with 2-digit values and between 80 and 100 multiplied.
5. 3 random numbers added or subtracted.''')
question = int(input("Choose the type of question you want: "))
print("\n")
if question == 1:
answer = int(input(str(value_a) + " + " + str(value_b) + " : "))
if answer == value_a + value_b:
print("Dayum quicc mafs, trie again if yu wand.")
else:
print("Bed mafs.")
elif question == 2:
answer = int(input( "(" + str(value_a) + "+" + str(value_b) + ")"+ "*2" + " : "))
if answer == 2*(value_a + value_b):
print("Dayum quicc mafs.")
else:
print("Bed mafs, trie again.")
this is the part where my answer never seems to be right:
elif question == 3:
answer == int(input(str(value_c) + " * " + str(value_d) + " : "))
print(value_c, value_d)
if answer == value_c * value_d:
print("Dayum quicc mafs.")
else:
print("Bed mafs, trie again.")
In Python, a single = means assignment. You used a double == which is an equality operator. Don't confuse the two, since the answer is not being assigned. Below I have changed the == to =.
elif question == 3:
answer = int(input(str(value_c) + " * " + str(value_d) + " : "))
print(value_c, value_d)
if answer == (value_c * value_d):
print("Dayum quicc mafs.")
else:
print("Bed mafs, trie again.")
Learning the basics of python and I am running across a problem. I'm sure it is a simple fix. I'm trying to get my program to get 20 different inputs before calculating the min, max, etc.
def main():
number = valueInput()
display(number)
def valueInput():
print("Please enter 20 random numbers")
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
def display(number):
print("The lowest number is:", min(number))
print("The highest number is:", max(number))
print("The sum of the numbers is:", sum(number))
print("The average number is:", sum(number)/len(number))
main()
I can get it to work by repeating this line:
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
to the 20th number but there must be a way that makes it shorter and cleaner. Again I am a beginner so any explanation would be much appreciated.
All you have to do to fix your program is remove four spaces:
def valueInput():
print("Please enter 20 random numbers")
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
The last line in the block above has been unindented by one level. Instead of returning after the first time through the loop, it lets the loop complete, and then returns the resulting list.
Also you can change your:
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
part with:
values = [int(input("Enter a random number " + str(x + 1) + ": ")) for x in list(range(20))]
return values
You can check out build in functions
Write a function called random_equation that takes as input a single parameter, the number of operators to generate in the random equation, and returns a string representing a random math equation involving the numbers 1-10 and operators +, -, *.
def random_equation(num):
result = ""
for i in range (num):
number = random.randint(1, 10)
if number == 1:
num_gen = (" 1 ")
elif number == 2:
num_gen = (" 2 ")
elif number == 3:
num_gen = (" 3 ")
elif number == 4:
num_gen = (" 4 ")
elif number == 5:
num_gen = (" 5 ")
elif number == 6:
num_gen = (" 6 ")
elif number == 7:
num_gen = (" 7 ")
elif number == 8:
num_gen = (" 8 ")
elif number == 9:
num_gen = (" 9 ")
else:
num_gen = (" 10 ")
operator = random.randint(1,4)
if operator == 1:
op_gen = (" + ")
elif operator == 2:
op_gen = (" - ")
else:
op_gen = (" * ")
math = result + num_gen + op_gen
I don't really know where to put the [i] to get it to repeat the loop since number is an integer and num_gen is the result
There are several issues here.
Your math gets replaced every time (e.g. "" + " 2 " + " - "), thus you never get to build longer sequences. You want to make result longer in each iteration: result = result + num_gen + op_gen, instead of always creating a new math with an empty result.
You never return anything. You will want to return result when the loop is finished.
You are now generating num numbers and num operators; that will produce equations like 1 + 3 *, which are a bit unbalanced. You will want to put only num - 1 pairs, and then one more number.
There are easier ways to make a string out of a number; str(num) will do what you do in twenty lines, just without the spaces.
With operator from 1 to 4, you will be generating as many * as you do + and - combined. Intentional?
import random
question = str(random.randint(-10,100)) + random.choice(op) + str(random.randint(-10,100)) + random.choice(op) + str(random.randint(-10,100))
q_formatted = question.replace('÷','/').replace('×', '*')
answer = eval(q_formatted)
print(answer)