Stack Overflow Asked on December 23, 2021
I’m new to this so I apologize for any of my mistakes.
This is my code:
def main():
global user_input
user_input = [0,1,2]
final = fibonacci(user_input)
return final
def append_fibonacci(integer_list):
new_list = []
while integer_list[1] < 40:
integer_list[0], integer_list[1] = integer_list[1], integer_list[0]+integer_list[1]
new_list.append(integer_list[0])
return(new_list)
def fibonacci(max):
final = append_fibonacci(max)
print("Enter a non-negative integer >")
print("The Fibonacci series starts with:",final)
My error is the outcome only prints:
The Fibonacci series starts with: [1, 1, 2, 3, 5, 8, 13, 21, 34]
When it’s suppose to print whatever other inputs (such as inputs: 8
/ outputs: The Fibonacci series starts with: [1, 1, 2, 3, 5, 8]
, inputs: 0
/ outputs: The Fibonacci series starts with: []
, inputs: 1
/ Output: The Fibonacci series starts with: [1, 1]
& inputs: six
/ Output: six is not a non-negative integer
).
I’m unsure if and where to use the (if, else statements)
and if to check if it (.isdigit)
or (isinstance, str)
.
Can someone help me or give me the answer? Thank you for your time in assisting me.
This is the question below.
The main
function must input a string. If the string does not represent a non-negative integer, then it should print a warning message. If it does represent a non-negative integer, then it should call the fibonacci
function to create the list
of Fibonacci numbers that are less than or equal to the non-negative integer and output this list
. The fibonacci
function should call the append_fibonacci
function multiple times to create the Fibonacci list
.
If you use a function from an imported module in your function, you must import that module inside your function.
def main():
user_input = input("Enter a non-negative integer >")
try:
if int(user_input) < 0:
print('{} is not a non-negative integer'.format(user_input))
else:
final = fibonacci(int(user_input))
print("The Fibonacci series starts with:", final)
except ValueError:
print('{} is not a non-negative integer'.format(user_input))
def append_fibonacci(integer_list):
if len(integer_list) < 2:
integer_list.append(1)
else:
integer_list.append(integer_list[-1] + integer_list[-2])
return integer_list
def fibonacci(max):
integer_list = []
if max == 0:
return integer_list
else:
while len(integer_list) < 2 or integer_list[-1] + integer_list[-2] <= max:
integer_list = append_fibonacci(integer_list)
return integer_list
Answered by Mark on December 23, 2021
The first problem is with this statement while integer_list[1] < 40
. Independent of the input you pass to the function it will output Fibonacci numbers less than 40. Second problem is the user_input
list you pass through functions which is completely unnecessary. You just need to pass max
number and your program will output Fibonacci numbers less than or equal to max
. Third problem is you do not have a mechanism to check whether the input is a valid non-negative integer.
Here is the code:
def main():
_max = ""
while not _max.isnumeric():
_max = input("Enter a non-negative integer >")
_max = int(_max)
final = append_fibonacci(max)
print("The Fibonacci series starts with:",final)
return final
def append_fibonacci(_max):
if _max==0:
return []
fibonacci = []
x=1
y=1
while y <= _max:
x, y = y, x+y
fibonaccis.append(x)
return fibonacci
main()
Answered by taha on December 23, 2021
Get input from users, you should use input()
method. In main()
function, user_input = input("Enter a non-negative integer >")
will work. Also avoid using global variables. You don't need input variable as a global variable in this code, simply delete that line.
Use int()
to check if the input is integer or not. You can handle the error message using try...except
. Use if...else
to check if the number negative or not.
Your code should look something like this:
def main():
user_input = input("Enter a non-negative integer > ")
try:
if int(user_input) < 0:
print('{} is not a non-negative integer'.format(user_input))
else:
final = fibonacci(int(user_input))
print("The Fibonacci series integer_lists with: ", final)
except ValueError:
print('"{}" is not a non-negative integer'.format(user_input))
def append_fibonacci(integer_list):
if len(integer_list) < 2:
integer_list.append(1)
else:
integer_list.append(integer_list[-1] + integer_list[-2])
return integer_list
def fibonacci(max):
integer_list = []
if max == 0:
return integer_list
else:
while len(integer_list) < 2 or integer_list[-1] + integer_list[-2] <= max:
integer_list = append_fibonacci(integer_list)
return integer_list
Answered by eren on December 23, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP