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 3 years ago.
Improve this question
A program to input (n) number :
First, I print the cube from each negative entry number
But the problem with step two is I'm trying to multiply numbers together and print the result
x=int(input("Enter number: "))
n=0
while x:
if x<0 :
s=x**3
print(s)
if x==0 :
s=x
print(s)
else :
s=x
print(s)
x=int(input("Enter number: "))
This question is a Python training that exists in many curricula that I have seen before
i think that is what do you want
mmult = 1
x = int (input("Enter number: "))
while True :
if x < 0 :
s = x **3
print (s)
if mmult != 0 :
mmult = mmult * x
print (mmult)
else:
mmult = x
print (mmult)
x = int(input("Enter number: "))
Related
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
import random
x = random.randint(0,50)
if int(input()) == x:
print ("yes")
else:
print ("no")
I want to make a guessing game, but I don't want the number to reset after 1 guess.
Use a while loop:
import random
x = random.randint(0,50)
while int(input("Enter a number: ")) != x:
print ("No! Try again.")
else:
print("Yes!")
import random
x = random.randint(0,50)
incorrect = True
while incorrect:
if int(input()) == x:
print ("yes")
incorrect = False
else: print ("no")
Here is another approach that allows you to exit the loop at will:
import random
x = random.randint(0,50)
guess = None
while guess != -1:
guess = int(input("Enter a guess or -1 to exit: "))
if guess == x:
print ("yes")
break
else:
print ("no")
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
I am trying to make it so that the user can input as many positive numbers as they want but when a negative is inputted it ends the loop and outputs the largest number inputted.
This is what I have so far
num = int(input("Enter a number: "))
while (num >= 0):
num = int(input("Enter a number: "))
if (num < 0):
print("Largest number entered: " + str(num))
Like that i guess
maxnum = int(input("Enter a number: "))
while True:
num = int(input("Enter a number: "))
if num < 0: break
maxnum = num if num > maxnum else num
if (num < 0):
print("Largest number entered: " + str(maxnum))
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 3 years ago.
Improve this question
It's my 2nd week in programming in Python and have never programmed anything before. appreciate step by step.
I don't know where to start.
try:
count = 0
while True:
user = int(input('Insert Number: '))
count += user
if user == 0:
break
print(count)
except ValueError:
print('Please Insert Numbers Only!')
Here a start, use a while loop for the input. I'll leave the summation part for you unless you need further help:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
while cc != 0:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
def is_int(num):
try:
return int(num)
except:
return False
total = 0
while True:
in_user = is_int(input("input integer: "))
if in_user is not False:
total += in_user
if in_user is 0:
break
print(total)
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 4 years ago.
Improve this question
I am wondering how to create different output with one input statement in a for loop (see my code below). For instance, how should I make each input statement read Enter number 1:, Enter number 2: and so on. Should I have only one input statement or multiple?
times = int(input("Enter how many numbers you want to sum"))
sum = 0
for i in range(0, times):
numInput = int(input("Enter number"))
sum = sum + numInput
print("The total is", sum)
One input is enough because you can change the string in your input prompt like this:
times = int(input("Enter how many numbers you want to sum? "))
sum = 0
for i in range(1, times+1):
numInput = int(input("Enter number {}:".format(i)))
# or use below code:
# numInput = int(input("Enter number %d:" % (i)))
sum = sum + numInput
print("The total is", sum)
To learn more about formatting python strings please refer to this link:
https://pyformat.info/
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
Instructions: Program needs to ask the user for a number. For example "5". The program outputs the number 15, as 1+2+3+4+5=15.
I am a novice and am stuck at the beginning:
n = (input("Insert a number: "))
while n != 0:
Please guide me what to do further
You can do it like this:
num = int(input("Choose a number: "))
total = sum(range(num + 1))
If you HAVE to do it using a while loop, you can do it this way:
total = 0
counter = 0
max = int(input("Choose a number: "))
while counter <= max:
total += counter
counter += 1
print(total)
n = int(input("Insert a number: "))
nums = range(1,n+1)
print sum(nums)
if you want to do the same thing with while loop:
n = int(input("Insert a number: "))
sum =0
while n>0:
sum+=n
n-=1
print sum
Maybe you can use something like this code:
try:
nr_in = int(input("Enter some number: "))
nr_out = 0
tmp = 0
while tmp < nr_in:
tmp += 1
nr_out += tmp
print(nr_out)
except:
print("This is not a number!")
This isn't the shortest and most pythonic way, but I think it might be easier to understand for you.
Hope this helps!
Do you use python 3 or 2? In python2, raw_input is recommended.
Basically all you need is to convert the string to numeric value ...
inp = input("number: ")
try:
n = int(inp)
except ValueError:
print("Please give me a number")
sys.exit(1)