Printing a character each second [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am fairly new in python and I would like to print a character each second. What I normally see in StackOverFlow are steps to print the characters inside a list per second. What I want to do is basically generate like "*" each second. Let me illustrate.
time = int(input("enter time:...") #user will input any integer as reference for the total time.
# * printed after 1 second
# * //another one printed after 2 seconds
# ...
Thank you!

well, I'm guessing you want to get a total time from the user and print a character with a time interval of 1 second that adds up to the total time?
if that's the case here's the code,
import time #this module helps us use the cpu's time in our python programe
time_limt = int(input("enter your total time: ")) #make sure you dont name this variable as time as it would mess up the refrences
char = str(input("enter the chracter you want to print: "))
for i in range(time_limit): #this is for printing up to the time limit
time.sleep(1) #this stops the programe for 1 second
print(char)```

Maybe, I am not sure your question. I guess you want to ask how to print a charactor by limiting time, every other one second. For example:
import time
print(*)
time.sleep(1)
if you have any question, please tell me, maybe I can help you. Thanks for your reading.

def wait_k_seconds():
import time
k = int(input("enter time: "))
for i in range(k-1):
time.sleep(1)
print('*' * (i + 1), end = '\r')
time.sleep(1)
print('*' * k)

Related

What to do for this loop? Write code that assigns the average of the numbers from 1 to n (where n is a positive integer value) to the variable avg [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Write code that assigns the average of the numbers from 1 to n (where n is a positive integer value) to the variable avg. The answer is specifically asking for a for loop but idk how to start it off. Does it have to be a for x in range... or is it something else. plz help I'm just starting my first programming class and idk what to do.
I tried to enter avg = (1+n)/2 but its not excepting that and with the lesson being on for loops I'd assume I would need to make a for loop.
I think this solves your problem, although you should try to understand what is going on so you may apply your learning in the future.
Here's what I wrote:
n=int(input("Enter number: "))
avg=sum([x for x in range(1,n+1)])/len(range(1,n+1))
print(avg)
This takes input in line 1, sums all the numbers between 1 and n in line 2, and prints out the value in line 3.
To break it down: line 1 takes input. the int statement makes it into a number rather than a string, which is what normally comes out of an input.
Line 2:
This line is where the for loop comes in. I have compacted mine into a generator statement, but thats not absolutely necessary. First, the generator statement puts all the numbers 1 to n into a list. It then sums up all of the variables in the list, and assigns it to avg.
This is what it would look like uncompacted:
list_var=[]
for x in range(0,n+1): # Range returns numbers between the min and max, but not including the max. Therefore, put a +1 afterwards to ensure it includes the max.
list_var.append(x) # put the variable in the list
sum_of=sum(list_var) #sum returns the sum of all the items in the list.
avg=sum_of/len(range(1,n+1) #avg is the average (1-n)/length (which is the length of the range)
Finally, the print statement logs it to the console.
print(avg)
Ta-da
That python explanation helped clear things up more than my chapter. Ty!
total = 0
for i in range (1,n+1):
total+=i
avg = float(total)/n

connecting and separating string inputs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
need help with taking inputs in a a loop like so.
example = input("enter the example !")
Then I need to add that input to one single variable and later print it all out
on separate lines EG:
loop cycle 1:
enter the example ! test1
loop cycle 2:
enter the example ! test2
loop cycle 3:
enter the example ! test3
inputs:
1. test1
2. test2
3. test3
One thing is that I am unable to use .append due to using lists in my case is
not in max efficiency. (probably will need to be taught to use \n)
you can append new line character to input function
for python2.
example = ""
for i in range(1,4):
example = example + str(i)+". " +raw_input("enter the example !") +"\n"
print example
for python3
example = ""
for i in range(1,4):
example = example + str(i)+". " +input("enter the example !") +"\n"
print (example)
output
messi#messi-Hi-Fi-B85S3:~/Desktop/soc$ python sample.py
enter the example !text 1
enter the example !text 2
enter the example !text 2
1. text 1
2. text 2
3. text 2
You can append the additional inputs onto the same string using the += operator
You will however need to declare the variable before using += for the first time
For example:
example = ""
for num in range(1,4):
print "loop cycle {}:".format(num)
example += "{}. {}".format(num, input("enter the example !"))
if num < 3:
example += "\n"
print example
edit: updated to include conditional new lines and input numbers

How do I add input to a if funtion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How exactly do I add an additional input to this if function, or atleast add a function which will allow me to give the user another option. Thank you, in advance.
ask = input ("Would you like to 1 input an existing number plate \n or 2 view a random number? 1 or 2: ")
if ask == "1":
print("========================================================================")
ask = input()("Please enter it in such form (XX00XXX): " )
If this ask = input()("Please enter it in such form (XX00XXX): " ) is actually in your code, just changing it to ask = input("Please enter it in such form (XX00XXX): " ) will fix your problem. You had one extra pair of brackets.
DISCLAIMER: This works in Python 3.x
For Python 2, use raw_input("Your question") instead of input("Your question").
As an answer to the question in your own answer:
What is wrong in this code?
ask = input ("-Would you like to 1 input an existing number plate\n--or 2 view a random number\n1 or 2: ")
if int(ask) == 1:
print("========================================================================")
while True:
ask2 = input("Please enter it in such form (XX00XXX): " ).lower()
if re.ask3("[a-z]{2}[0-9]{2}[a-z]{3}",ask2):
print("Verification Success")
break
else:
print("Verification Failed")
You have to indent the while loop once (default is 4 spaces) and everything inside of the while loop twice (default 8 spaces). When pasting a code like this, make sure to include all code needed to run. In this case, show your import re (I had to find out myself you imported that).
I hope I helped.
You are comparing an int(1) to str("1").
It should be
if int(ask) == 1:
# rest of your code
or you can use raw_input() function which returns a string value

Write a Python program that repeatedly asks the user to input coin values until the total amount matches a target value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
However, I realized that I don't actually know how to do this myself without examining every possible combination of coins. There has to be a better way of solving this problem, but I don't know what the generic name for this type of algorithm would be called, and I can't figure out a way to simplify it beyond looking at every solution.
I was wondering if anybody could point me in the right direction, or offer up an algorithm that's more efficient.
You can try something like this:
MaxAmount = 100
TotalAmount = 0
while TotalAmount < MaxAmount:
#Here if you want it to be more precise on decimals change int(raw_input("Amount: ")) to float(raw_input("Amount: "))
EnteredAmount = float(raw_input("Amount: "))
if EnteredAmount > MaxAmount:
print "You can not go over 100"
elif TotalAmount > MaxAmount:
#You can go two ways here either just set TotalAmount to MaxAmount or just cancel the input
print "You can not go over 100"
elif EnteredAmount <= MaxAmount:
TotalAmount = TotalAmount + EnteredAmount
print TotalAmount
print "You have reached the total amount of ", MaxAmount
Could use a loop into an if - elif - else statements
e.g. populate a variable with your amount, then using this for the loop condition keep asking to take away coin amounts until you reach 0

Python Help Again [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am using Python 3.2 Just so you know what I am doing, here is the assignment:
~The function random.randint from the random module can be used to produce an integer from a range of values. For example, random.randint(1,6) produces the values 1 to 6 with equal probability. A simple tutoring program may randomly select two numbers between 0 and 12 and ask a question such as the following: What is 6 times 8? Upon receiving user response, the computer checks to see if the answer is correct and gives encouraging remarks. Write a program that will loop 10 times producing questions of this form and gives a score for the user at the end.
Here is what I have in my program:
print ("Hello. Let's begin")
for i in range (1,10):
from random import randint
x=randint (0,12)
y=randint (0,12)
print (x,"*" y,"=?")
product= int(input ("What is the product?")
if (product==x*y):
print ("Awesome! That is correct!")
else:
print ("Sorry, that is not correct, but let's try another one!")
I have everything working with all of this. It asks the user a random multiplication question and responds ten times. What I do not understand how to do is to give the user a score at the end. I'm brainstorming ideas and not much is really working. I think I would have to do something like:
score=
But I don't know how to tell the program to calculate the number of correct answers... Do I say score=number of if?
And then when I print the score I can just say:
if (score>5) :
print: ("Great job! You scored a",score,"out of ten!")
else:
print: ("Not the best score, but you can try again! You scored a",score,"out of ten.")
Or is there maybe an easier way to do this?
It seems like it would be simplest to just make a new variable ("score" or suchlike) and initialize it as 0 before the loop. Then, when you check if a user was correct, just increment it by one if it was right, and leave it alone if it was wrong.
Hope this helps!
First, set score to 0
score = 0
then in the loop, try something like
if (product==x*y):
print ("Awesome! That is correct!")
score += 1
else:
print ("Sorry, that is not correct, but let's try another one!")
the important part being the score += 1 this increases the score by one when you get a correct answer. You can the put your score > 5 in after the loop.

Categories