Looping a whole python function until true [closed] - python

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))

Related

Why is there a SyntaxError Python [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 3 months ago.
Improve this question
I wrote this code snippet:
lowestNumber = int(input("\nWhat would you like your lowest number to be?"))
highestNumber = int(input("What would you like your highest number to be?"))
number = random.randint(lowestNumber, highestNumber)
tries = 0
while tries < 10:
guess = int(input(f'\nEnter a number between', lowestNumber))
if guess == number:
print("You guessed correctly! The number was", number)
break
elif guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
tries += 1
SyntaxError: bad input on line 22 in main.py.
Line 22 was guess = int(input(f'\nEnter a number between', lowestNumber)).
I searched it up on google and got nothing, I pasted it into OpenAI's code fixing and it also didn't help.
How can I fix this error?
When you wrote
guess = int(input(f'\nEnter a number between', lowestNumber))
it passed both the string and lowestNumber into the input function. However, you probably wanted to write something like Enter a number between (lowestNumber) and (highestNumber). To do this, you would have to write
guess = int(input(f'\nEnter a number between {lowestNumber} and {highestNumber}. '))
In my example, it passes in one object, the string, which contains lowestNumber and highestNumber in it. In your example, it passes in two objects, the string and lowestNumber.
The formatting you did in the input functions works in print statements, so the print statements are correct, but the input function is not.

How to make the code not end after inputing 1 number? [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
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")

I've completed the code however I don't understand what's wrong [closed]

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?")

How would I print the largest number from a user input (python) [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
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))

Receiving integers from the user until they enter 0 [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 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)

Categories