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")
Related
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 2 years ago.
Improve this question
Question is: Use procedure with parameter. Ask the user to input numbers until they say “no”. Output if each number is greater than or less than or equal to 5.
def output(number):
if number > 5:
print("Greater than 5")
elif number < 5:
print("Less than 5")
else:
print("It is equal to 5")
userInput = "yes"
print(userInput.lower() != "no")
num = input("Enter a number \n")
output(userInput)
userInput = input("Would you like to try again?")
You're trying to compare a str with an int. The following will fix it:
def output(number):
if number > 5:
print("Greater than 5")
elif number < 5:
print("Less than 5")
else:
print("It is equal to 5")
userInput = "yes"
print(userInput.lower() != "no")
num = int(input("Enter a number \n"))
output(userInput)
userInput = input("Would you like to try again?")
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: "))
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 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 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)