while loops, how to set things equal and break while loop? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am in Ap computer science( i will never touch CS after this ugghhh) and have to do this python problem using While loops. The problem is:
"As a future athlete you just started your practice for an upcoming event. On the first day you run x miles, and by the day of the event you must be able to run y miles.
Calculate the number of days required for you to finally reach the required distance for the event, if you increases your distance each day by 10% from the previous day.
Print one integer representing the number of days to reach the required distance"
and so far i've come up with this:
x = int(input())
y = int(input())
days=1
while x<y:
x=x*0.1
days= days+1

you should increment your day then print it out your while loop, also only multiplying by 0.1 is like dividing by 10 so you should not forget to add your x value, try like this:
x = int(input("On the first day you run: \n"))
y = int(input("and by the day of the event you must be able to run:\n"))
day = 1
while x<y:
x += x+x*0.1
day += 1
print(f'you should run {day} days')

Related

Printing a character each second [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 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)

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

Find the time for more than K customers present in the hotel [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 2 years ago.
Improve this question
if there are more than K customers present in the hotel at any given moment ,then period of time is called P period.
Task is to determine the P period
Input format
first line contains n and k and next n lines contain check-in and check-out time of the ith customer
Test Case:
3 2
5 8
2 4
3 9
Output:4
If I am not wrong we have to find the time at which more than K customers present at the moment.
my code
def hotel(n,k,A):
count=0
dp=[1]*n
for i in range(n):
I1=A[i][0]
o1=A[i][1]
time=[]
for j in range(i+1,n):
I2=A[j][0]
o2=A[j][1]
if I1>=I2 and I1<o2:
dp[i]=dp[i]+1
if o1<=o2:
time.append(o1-I1)
else:
time.append(o2-I1)
elif I1<I2 and o1>I2:
dp[i]=dp[i]+1
if o1>o2:
time.append(o2-I2)
else:
time.append(o1-I2)
if dp[i]>=k:
count+=sum(time)
return count
Problem it showing wrong answer with code
can anyone help.
The sample result (4) suggests that we are looking for the total number of days where at least K (not more than) customers are present. The problem statement also doesn't specify if the check out day is included or excluded from the periods (which gives the same answer for this data but may differ on other samples).
In any case, you can compute these values using list comprehensions and sums:
n = 3
K = 2
A = [(5,8),(2,4),(3,9)]
checkIns,checkOuts = zip(*A)
firstDay = min(checkIns)
lastDay = max(checkOuts)
counts = [ sum(day in range(inTime,outTime) for inTime,outTime in A)
for day in range(firstDay,lastDay+1) ]
P = sum(count >= K for count in counts)
print(P) # 4
The counts list is built by going through every possible day and counting, for each day, how many customers were present. Counting customers for a given day goes through the check-in/check-out times (in A) to check if that day is within the customer's presence period.
note: range(inTime,outTime) above assumes that check-out days are excluded. Change it to range(inTime,outTime+1) if they are to be included

Antonia and David are playing a game. Each player starts with 100 points [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 2 years ago.
Improve this question
Question: Antonia and David are playing a game. Each player starts with 100 points. The game uses standard six-sided dice and is played in rounds. During one round, each player rolls one die. The player with the lower roll loses the number of points shown on the higher die. If both players roll the same number, no points are lost by either player. Write a program to output the final scores and the winner
Input Specification The first line of input contains the integer n (1 ≤ n ≤ 15), which is the number of rounds that will be played. On each of the next n lines, will be two integers: the roll of Antonia for that round, followed by a space, followed by the roll of David for that round. Each roll will be an integer between 1 and 6 (inclusive). Output Specification The output will consist of two lines. On the first line, output the number of points that Antonia has after all rounds have been played. On the second line, output the number of points that David has after all rounds have been played.
One of my many problems is making the program list the correct number of inputs the first input specifies.
Here is what I have so far:
I know I only asked for one thing specifically, but can anyone complete this challenge so I can see what I can add to my program
Because it is a homework question, you really should try to it yourself first. With this being said, I will give you hints but I will not give you a full working program - I hope you can understand my reasoning for this.
To start, this problem definitely calls for some type of iteration as rolling a dice for n amount of times is repetitive. Whether you choose a for loop or a while loop is up to you - in this example I use a while loop. After getting the amount of rounds (don't forget to convert the user input into int), you can write something like this:
while rounds > 0:
# simulate rolling here
rounds -= 1
Rolling a dice is a random action - there is a 1/n chance to roll a number where n is the number of sides on the dice. I would suggest creating a list of all possibilities:
dice = [1,2,3,4,5,6]
And then use choice() from the random module to select a random item from this list.
from random import choice
david_roll = choice(dice)
antonia_roll = choice(dice)
Now that you have the values of each roll, you can just perform some simple comparison on the rolls:
if david_roll > antonia_roll:
# modify scores accordingly
elif david_roll < antonia_roll:
# modify scores accordingly

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

Categories