Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
im new to python and i have a count program that i dont know whats wrong with, for some reason, when trying to run it, when i put in Y or N for slow count, it just does nothing and exits with code 0.
Heres my code:
def cnt(start, end):
for x in range(start, end+1):
print(x)
time.sleep(1)
print("Done!")
def count():
num = int(input("Enter a number to count to: "))
#Slow count meaning counting 1 by 1 in console.
slcnt = bool(input("Would you like to slow count? Y/N: "))
if slcnt == "Y":
cnt(0, num)
elif slcnt == "N":
for i in range(num):
print(i)
print("Done")
count()
The problem is this line slcnt = bool(input("Would you like to slow count? Y/N: ")), you can't make it boolean because you are asking for a character. It may be fixed like this:
import time
def cnt(start, end):
for x in range(start, end+1):
print(x)
time.sleep(1)
print("Done!")
def count():
num = int(input("Enter a number to count to: "))
#Slow count meaning counting 1 by 1 in console.
slcnt = input("Would you like to slow count? Y/N: ").upper()
if slcnt == "Y":
cnt(0, num)
elif slcnt == "N":
for i in range(num):
print(i)
print("Done")
count()
You didn't add the line import time, but I guess it was a mistake when you pasted the code.
I also added upper() to the input function, so the script will accept Y/N and y/n.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I am trying to make this code break when the user enters "X" and allow it to continue when the user presses enter.
With what I have here, it stop and waits for enter input to output the next name I want it to output but doesn't break when I input X.
def Main():
userSent = input("Enter X to quit ").upper()`
while True:
if userSent == "X":`
break
else:
print(GenName())
input()
I tried getting rid of the input to fix the problem but then it just continuously went on nonstop. I expected it to break on X or else print GenName() and stop to wait for input.
The problem is that you only define userSent once, right before the loop starts. That means that if the first thing you enter isnt a capital X, then the program will never end. Try doing this:
def Main():
while True:
userSent = input("Enter X to quit ").upper()
if userSent == "X":
break
else:
print(GenName())
Within your loop, userSent is never updated. If the loop enters, it will never exit.
I suspect your last line is meant to be something like userSent = input().
You assign userSent value but you never updated it inside the while loop. You can use either of these versions:
def Main():
userSent = input("Enter X to quit ").upper()
while userSent != "X":
print(GenName())
userSent = input().upper()
def Main():
userSent = input("Enter X to quit ")
while userSent != "X" and userSent != "x":
print(GenName())
userSent = input()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to create a game of last man standing in python, but when you input a number it outputs the negative version of the correct answer? Also, when i enter anything other than 1, 2, or 3, I get the error: ValueError: could not convert string to float: '(anything other than the numbers)'
any help is appreciated. Thanks!
import random
num = random.randrange(20, 30)
print ("The number is " + str(num) + ", take either one, two or three away from it!")
take = float(input("input either 1, 2 or 3: "))
newnum = take - num
if take == 1:
print(newnum)
elif take == 2:
print(newnum)
elif take == 3:
print(newnum)
else:
print("please enter either 1, 2 or 3!")
You could just use a while loop to make sure the user only inputs 1,2 or 3.
import random
num = random.randrange(20, 30)
print ("The number is " + str(num) + ", take either one, two or three away from it!")
take = None
while take not in {1,2,3}: #{} faster than ()
take = int(input("input either 1, 2 or 3: "))
print("please enter either 1, 2 or 3!")
print(num-take)
Also as the above code shows, you need to use num-take instead of vice-versa.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm getting a syntax error on my 2nd while loop. Can't figure out why, any help appreciated :)
#intro
print("Welcome to my prime number detector.")
print("Provide an integer and I will determine if it is prime.")
#again loop
again = "Y"
while again == "Y":
num = (int(input("Enter an integer"))
#check for valid input
while num < 1:
num = (int(input("Enter an integer"))
#test for prime
for d in range(2,num):
if (num % d) == 0:
print(num,"is not prime.")
else:
print(num,"is prime.")
#ask again
again = intput("Do you want to play again? (Y/N)")
You are missing a closing parenthesis ) in the two of your following lines. The correct line of code is
num = (int(input("Enter an integer")))
Also, as sheepez mentioned below, your outer brackets are redundant. You can simply use
num = int(input("Enter an integer"))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to make a random number guessing game but I cant get the if statement to check if the users input is = to the random number
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = print(input("Guess the number from 1 to 50: "))
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
The unintended behavior of your program is due to this line:
myNumber = print(input("Guess the number from 1 to 50: "))
Here, you are trying to assign myNumber to the return value of the print statement (Which is None) and not the value obtained from the input() statement. To fix this, simply remove the print() around the input.
myNumber = input("Guess the number from 1 to 50: ")
Hope this helped!
You don't need the print statement around input.
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = input("Guess the number from 1 to 50: ")
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
Note that this code will not work if the user enters something besides an integer, because the int() call will not cast correctly
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 6 years ago.
Improve this question
num = input(float("Enter a number (enter 'q' to quit): ")
p=0
while num>=1:
p=p+1
Why do I keep getting a syntax error on the first p?
Does this mean I can't assign a numerical value on a variable?
You should close the bracket
num = input(float("Enter a number (enter 'q' to quit): "))
import sys
num = input("Enter a number (enter 'q' to quit)")
try:
num = int(num)
except:
if num == 'q':
sys.exit()
p=0
while (num>=1):
p=p+1
num = num - 1 #otherwise it will go to infinite loop