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 7 years ago.
Improve this question
I wrote two functions. First one, when called checks if the number is three-digit, and second one is checking if the number is even or odd:
def even(n):
if n%2==0:
print('number {} is even'.format(n))
else:
print('number {} is not even'.format(n))
return
def main(n):
kraj=True
while(kraj):
a=int(input('three-digit number pls: '))
if (a>99 and a<1000):
kraj=False
return True
else:
print('I said three-digit number!! ::')
return False
main(0)
What my problem is, when I call function even like even(a), it gives me an error saying a is not defined.
You must return a in the main() function and pass it to the even function.
def main(n):
kraj=True
while(kraj):
a=int(input('three-digit number pls: '))
if (a>99 and a<1000):
kraj=False
return a
else:
print('I said three-digit number!! ::')
return False
a = main(0)
even(a)
You have to return the value of a so you can use it somewhere else
Also you have a while and return in if and else.
Therefore you do not need your while loop. Or you want the user to reenter on error you then have to get rid of the return in else
You should give your function a more meaningfull name like read_threedigit_number
You normally don't put paranthesis around if and while
Improved Code
def read_threedigit_number(n):
while True:
a=int(input('three-digit number pls: '))
if a>99 and a<1000:
return a
else:
print('I said three-digit number!')
a = read_threedigit_number(0)
even(a)
Related
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 12 months ago.
Improve this question
The real problem is there:
write a program that takes a string as input and prints “Binary Number” if the string contains only 0s or 1s. Otherwise, print “Not a Binary Number”.
You can iterate every character of the input and verify it is a zero or a one. The all function can be used for this iteration and check.
Also require that the string has at least one character:
def isBinary(string):
return all(ch in "01" for ch in string) and string != ''
Example use:
string = input("Enter a binary number: ")
if isBinary(string):
print("Thank you!")
else:
print("That is not a binary number.")
It is easy.
def is_binary(string_to_check:str):
for x in string_to_check: #loop though all letter
if x in ('0','1'):
continue #yep the letter is binary thingy
else:
return False #no
return True #yep the string is binary
Next time check google before ask.
def check_if_binary(string) :
p = set(string)
s = {'0', '1'}
if s == p or p == {'0'} or p == {'1'}:
print("Binary Number")
else :
print("Not a Binary Number")
string = str(input ("Enter the sting : "))
check_if_binary(string)
References: https://www.geeksforgeeks.org/python-check-if-a-given-string-is-binary-string-or-not/
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 2 years ago.
Improve this question
word_1 = str(input(f"Enter a string: "))
word_reversed = word_1[::-1]
def check_palindrome(input_string):
if word_1 == word_reversed:
return (True)
print (f'{word_1} is a palindrome')
elif word_1 != word_reversed:
return(False)
print(f'{word_1} is not a palindrome')
print(check_palindrome(word_1))
> `Here is the output:
Enter a string: level
level is a palindrome
True`
I want to write retun true value but make it unvisible. I should run it with the return
This?
word_1 = str(input(f"Enter a string: "))
word_reversed = word_1[::-1]
def check_palindrome(input_string):
if word_1 == word_reversed:
print (f'{word_1} is a palindrome')
return (True)
elif word_1 != word_reversed:
print(f'{word_1} is not a palindrome')
return(False)
check_palindrome(word_1)
Instead of
print(check_palindrome(word_1))
You should just write
check_palindrome(word_1)
It will still print the line you expect since the function says to print it, but if you call the function in a print statement, it will always print the return value.
If you use return you assign a value to the function, printing that function first runs the function, and then prints it.
To solve this you could write : return str(word1, "is a palindrome") instead of returning True and False
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 2 years ago.
Improve this question
I was wondering if it's possible to get the only input from a def function.
def choose():
while True:
try:
pick = float(input("Enter any number that isn't 0 "))
if pick != 0:
break
else:
pick = float(input("Try again! Enter any number that isn't 0 "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
else:
break
choose()
I will try to be as clear as possible. Can you take the pick input from choose() and store it somewhere else. Say like when your done inputting the number you want.
Can you run:
print(pick + 15)
or you can't take the input from the choose() at all. I would just like to know. Cause if so I don't even know how to do so. So I would appreciate the advice.
You can't access local variables from outside the function. The function should return the value, and you can assign that to another variable.
def choose():
while True:
try:
pick = float(input("Enter any number that isn't 0 "))
if pick != 0:
return pick
else:
print("Try again. The number has to be non-zero!")
except ValueError:
print("Sorry, I didn't understand that.")
choice = choose()
print(choice + 15)
You also shouldn't ask for input in the else: block, because it's going to ask again when the loop repeats. Just print the error message there, without reading input.
You don't need the continue statement, since loops automatically continue unless the repetition condition becomes false (which can never happen with while True:) or you execute a break or return statement to leave the loop.
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 6 years ago.
Improve this question
Here is the problem: write a function that checks whether a string is valid password.
Rules: Must have at least 8 characters A password must consist of only letters and digits A password must contain at least 2 digits
I am having trouble with def CountsDigitsFor, I want it to check for at least 2 digits.
def getPassword():
return input("Enter password: ")
def CountDigitsFor(password):
res = []
for i in password:
if i.isdigit():
res.append(i)
return int
def validPassword(password):
if len(password) >= 8:
if password.isalnum():
if CountDigitsFor(password) >= 2:
return True
else:
return False
def main():
password = getPassword()
if validPassword(password):
print(password + " is valid")
else:
print(password + " is invalid")
main()
return int
is just returning the class int. What you want to return is the number of digits. That of course would be:
return len(res)
Why not just use this one:
def CountDigitsFor(password):
return sum(character.isdigit() for character in password)
I have provided the above in your other question, but just in case that wasn't clear I am going to give you a brief description on how this works.
The above is equivalent to what you want but in one single line. The idea is that you loop through the characters of the password, check if the character is a number and then get the sum of the digits in the password.
Then after you call the CountDigitsFor() you will get the number of digits in the password and you can check if they are at least 2 with the if statement you have provided.
if CountDigitsFor(password) >= 2:
return True
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 6 years ago.
Improve this question
I have this pseudocode that I need to translate:
Prompt the user to enter a string and call it s.
Let l be the length of string
For i from 0 upto l-1:
print s[0:i]
For i from 0 upto l-1:
print s[i:l]
Print a closing statement
This is my translation:
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in [0,L-1]:
print (s[0:i])
for i in [0,L-1]:
print(s[i:L])
print("This program is complete!")
main()
However, the code isn't printing correctly. Can someone help me find my error? Thank you.
You say for i in [0,L-1], but [0,L-1] is a list with two elements: 0 and L-1. What you want instead is range(0, L) or range(L):
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in range(L):
print (s[:i])
for i in range(L):
print(s[i:L])
print("This program is complete!")
main()