I have a function named orderInt passed three integers and returns true if the three int are in ascending order, otherwise false. Here's my code so far:
def orderInt(a, b, c):
print "Enter 3 integers: "
a = input()
b = input()
c = input()
How do I compare the variables?
First of all, your indentation is wrong.
def orderInt():
print "Enter 3 integers: "
a = input()
b = input()
c = input()
if a<b<c:
return True
else:
return False
print orderInt()
Second, your function is taking three arguments and also taking inputs. The arguments passed will be overwritten by your inputs.
def orderInt():
print "Enter 3 integers: "
if a<b<c:
return True
else:
return False
a = input()
b = input()
c = input()
print orderInt(a,b,c)
Hope this helps.
Related
This question already has answers here:
Why is this printing 'None' in the output? [duplicate]
(2 answers)
Closed 1 year ago.
str = input("Please enter y to start the program: ")
while str == 'y':
def printNTimes(s, n):
for i in range(n):
print(s)
phrase = input("Enter a string: ")
number = int(input("Enter a positive number: "))
allTogether = printNTimes(phrase, number)
print(allTogether)
print()
str = input("Please enter y if you want to continue: ")
print("DONE")
Output:
Please enter y to start the program: y
Enter a string: Hi
Enter a positive number: 3
Hi
Hi
Hi
None
Please enter y if you want to continue:
You don't need print(allTogether), or the variable itself, because when you print it, you get an extra None (because the function returns None i.e, it does not return anything).
Also, put the function outside the while loop so that it is easier to read.
def printNTimes(s, n):
for i in range(n):
print(s)
str = input("Please enter y to start the program: ")
while str == 'y':
phrase = input("Enter a string: ")
number = int(input("Enter a positive number: "))
printNTimes(phrase, number)
print()
str = input("Please enter y if you want to continue: ")
print("DONE")
Just call the function. You could use return and then print the function, but this might be easier.
The problem is the function printNtime is actually being used as a subroutine, meaning it doesn't RETURN any values.
I am not sure what you want as a final output so here's two solution.
IF USED AS SUBROUTINE: just remove the print(allTogether)
IF USED AS A FUNCTION: you need to create a string variable in the function and return it.
def printNTimes(s, n):
mystring = ""
for i in range(n):
mystring = mystring + s + "\n"
return mystring
The issue you're seeing is because your routine, printNTimes(), returns a None value. You say:
allTogether = printNTimes(phrase, number)
print(allTogether)
allTogether is set to None because printNTimes does not return a value. When you call print(allTogether), that's what is printing the None.
It sounds like you intended your printNTimes() routine to assemble one big string of all the output and return it, which you would then print out via the print(allTogether) call. But your printNTimes() routine is calling the print() method and outputing the text then. So, you need to rewrite your printNTimes() method. Also, note that defining that function inside the loop is not necessary. Once is sufficient. So something like this should suffice:
def printNTimes(s, n):
s_out = ""
for i in range(n):
s_out += s + '\n'
return s_out
str_ = input("Please enter y to start the program: ")
while str_ == 'y':
phrase = input("Enter a string: ")
number = int(input("Enter a positive number: "))
allTogether = printNTimes(phrase, number)
print(allTogether)
print()
str_ = input("Please enter y if you want to continue: ")
print("DONE")
Also note that I renamed your str variable to str_. str is a Python type, a reserved word that should not be reused.
Define a function generateNChars(a_nNum, a_Char) that takes an integer a_nNum and a
character a_Char and returns a string, a_nNum characters long
def generateNChars(a_nNum, a_Char):
for i in range (0, a_nNum):
print(a_Char, end = "")
nNum = int(input("Entyer a number: "))
strChar = input("Enter a character: "))
result = generateNChars(nNum, strChar)
print(result)
I got "None" after printing this code. How can I fix this??
You need to add return keyword to return the value. When you don't use a return keyword, it returns None by default. Therefore when you print the function, you receive None. Also, keep in mind that a loop ends when return is used. So, instead of using a for loop, you can multiply, which will return the string repeated n number of times.
def generateNChars(a_nNum, a_Char):
return a_Char*a_nNum
nNum = int(input("Entyer a number: "))
strChar = input("Enter a character: "))
result = generateNChars(nNum, strChar)
print(result)
You have to return the string from the function. Now, the returned string gets assigned to the result variable and you can print it.
def generateNChars(a_nNum, a_Char):
temp = ""
for i in range (0, a_nNum):
temp+=a_Char
return temp
nNum = int(input("Entyer a number: "))
strChar = input("Enter a character: "))
result = generateNChars(nNum, strChar)
print(result)
def fib_gen(fib=-1):
print(f"initial fib={fib}")
a = 1
b = 0
yield b
i = 1
print(f"initial i={i}")
while(i!=fib):#infinite sequence by default, since default value of fib is -1
c = a
a = a+b
b = c
i = i+1
print(f"i={i}")
print(f"fib={fib}")
print("is fib==i: ",fib==i)
input("Continue? ")
yield a
x = input("First how many fibonacci numbers do you want? ")
fibs = fib_gen(x)
print(f"x={x}")
try:
while(True):
print(next(fibs))
except:
print(f"That's the first {x} fibonacci numbers")
Even when fib and i become equal, the result shows False. Why?
And if I pass a concrete value in the generator function instead of a variable, then the result comes as expected. Why???
input() returns a string by default, to get a number do int(input())
x = int(input("First how many fibonacci numbers do you want? "))
This question already has answers here:
How do I check if raw input is integer in python 2.7?
(5 answers)
Closed 6 years ago.
# Validating for only numbers in python one after another(in sequence)
a = raw_input()
#if a is a number then it should take next raw_input i.e., 'b' else it should ask for 'a' again
b = raw_input()
#if b is a number then it should take next raw_input i.e., 'c' else it should ask for 'b' again
c = raw_input()
#if c is a number then it should take next raw_input i.e., 'd' else it should ask for 'c' again
## This should be done upto 'e'
use a.isdigit() to validate string a is digit or not
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.
Make use of if..else construct for the conditional checks and refer this post for the integer check.
Edit 1
I have not executed it,but you could try the following and see if it works-
def ask(text):
temp = raw_input(text)
try:
retVal = int(temp)
return retVal
except ValueError:
return False
a = False
b = False
c = False
while not a:
a = ask("Enter A:")
if a == False:
continue
while not b:
b = ask("ENter B:")
if b == False:
continue
while not c:
c = ask("Enter C:")
I have an issue with my python code to make beeping noises.
It just does an infinite loop of beeps even though it should stop.
import winsound
import time
z = 1
while z == 1:
b = input('Enter number of beeps required')
print(b)
a = input('Is this number correct?')
if a == "yes":
print('Python shall use this number')
z = 2
if a == "no":
b = input('Enter number of beeps required')
x = 1
y = -1
while x == 1:
freq = 1500
dur = 50
winsound.Beep(freq,dur)
y += 1
if y == b:
x = 2
Thanks for any help
If you are using Python 3.x, input() returns a string object.
Comparing string object with int always return False.
>>> '1' == 1
False
You should convert string object to int before compare them:
b = int(b)
BTW, it's better to use following instead of while loop:
for i in range(int(b)):
...
Your problem is that input() on Python 3 returns a string, so b will be a string, and therefore y == b will never be True.
Use
b = int(input('Enter number of beeps required'))
Change
b=input ('Enter number of beeps required')
to
b=int(input ('Enter number of beeps required'))
You are reading b as string and comparing it against an int in
if y == b:
which will never be True. Thats why your code is infinitely looping.