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)
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 10 months ago.
Improve this question
Im already done with the part before the highlighted part, but i can't understand the part which is highlighted and i dont want to look at the code.
Something like that:
number = int(input("Your number: "))
while number != 1 :
number = collatz(number)
As I understand, it should be something along the lines:
def collatz(number):
if number // 2 == 0:
print(number // 2)
return number // 2
else:
print(3 * number + 1)
def iterate_program:
number = int(input())
while number != 1:
number = collatz(number)
And then you just call iterate_program.
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 3 years ago.
Improve this question
I'm new to python and I want to archive a function as below where a input of a user gets checked with the arithmetic operators against the case of the random typed number being true to the statement number < 100 and when not being looped by the elif function.
So I want to check the first if statement but if that is not true it should go to the elif statement and then be checked by the if statement again until it fits the criteria.
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
number/10
userInput = int(input("Your number please: \n"))
print(unit(userInput))
SOLVED!
I could solve the problem by doing the following changes:
userInput = int(input("Your number please: \n"))
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number >= 100:
b = (number/10)
return unit(b)
print(unit(userInput))
use recursion
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
return unit(number/10)
userInput = int(input("Your number please: \n"))
print(unit(userInput))
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 requesting someone to tell me a program that asks a user to input only 9 digits. Number 0 not at start but having 9 digits.
How can I limit the number of digits?
For example:
Num=int(input('please Enter a Number'))
if num?!
Please help me soon.
If i got this right this should work:
a=input()
if len(a)==9 and a[0] != '0' and a.isdigit():
#if you want it to be integer
a=int(a)
do something
Hope it helps.
Well if I understood correctly you need a program that asks the user to enter exactly 9 digits. In that case it's pretty simple.
i = input("Enter a nine digit number: ")
if len(i) != 9:
print("You must enter 9 digits!")
quit()
if not i.isdigit():
print("Your input must be a number!")
quit()
# num is a 9 digit integer
# i is a 9 digit string
num = int(i)
You can do this.
num = input('Please enter a number')
if not num[0] == "0" and len(num) == 9 and num.isdigit():
# You can print a message or cast the num into an integer here
# like this: input_number = int(num)
print(True)
else:
print(False)
So what's happening here is that the program will accept the user input and evaluate if the first digit is NOT 0 AND that the length is exactly 9 characters.
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)
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 9 years ago.
Improve this question
I must create a dice game that generates numbers from 1 to 6. It will then throw the dice 50 times and it will count the number of odd numbers and even numbers. I'm using Python.
Here is my code:
import random
# Determine odd and even numbers
throws = 0
even = 0
odd = 0
maxthrows = 50
print "Even : Odd"
while True:
throws += 1
if throws == maxthrows:
break
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
print even, " : ", odd
raw_input("Press enter to exit.")
Your loop is wrong, it should be:
while throws != maxthrows:
throws += 1
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
Notice that:
Whenever possible, the exit condition should be used in the loop condition, not in an if ... break
The part where you ask if the dice is odd must be inside the loop, in Python indentation matters - a lot!