How to write Python program illustrating chaotic behaviour - python

When I run the program below, it outputs all the numbers as 0.0. How can fix this to illustrate chaotic behaviour?
# A simple program illustrating chaotic behaviour
def main():
print ("This program illustrates a chaotic function")
x = int (float(input("Enter a number between 0 and 1: ")))
for i in range(10):
x = 3.9 * x * (1-x)
print (x)
main()

In the line where you get input, you take the number and make it an integer. Making something an integer removes all decimals. When you type in ‘0.54’, then the int function takes off the decimals leaving just 0.
If you just do x = float(input("Enter a number between 0 and 1: ")) then it will work.
Have a nice day!

Actually u have a +1 or - 1 in end will help

Related

Unable to return the correct result of the function

I am just a beginner at learning code with no prior knowledge at all.
I wrote a simple code below to apply my knowledge of defining functions and a for loop to work. This code will supposedly allow the user to obtain any numeral to a power based on their input. However, as you will observe below, I will always get a result of 0.
I am honestly not sure where I went wrong and have been told that my code is not the most readable. As such, I will like to seek your help in enlightening me what is wrong with it and how to make it better.
Thank you very much guys!
def to_power(base, power):
result = 1
for base in range(power):
result = result * base
return int(result)
print(to_power(int(input("Enter Base Number: ")), int(input("Enter Power: "))))
Enter Base Number: 4
Enter Power: 4
0
Process finished with exit code 0
You were close. This is a working version of your code.
Problem with your code:
range(power) will generate values starting from 0 up to power-1. You were using base to iterate over these values and since you were using result = result * base, you were multiplying by 0 which is why you are getting 0
Solution:
You need to just iterate a loop for power number of times and keep multiplying base for that many (power) number of times. Since the index of the loop is unused here, I used _ in for _ in range(power):
def to_power(base, power):
result = 1
for _ in range(power):
result = result * base
return int(result)
print(to_power(int(input("Enter Base Number: ")), int(input("Enter Power: "))))
# Enter Base Number: 4
# Enter Power: 4
# 256
looks like this is what you want:
def to_power(base, power):
result = 1
for i in range(power):
result = result * base
return int(result)
Example:
to_power(4,4)
Out[5]: 256

Python: Adding odd numbers together from an input

Have a little problem. I'm writing a simple program that takes an input of numbers (for example, 1567) and it adds the odd numbers together as well as lists them in the output. Here is my code:
import math
def oddsum(n):
y=n%10
if(y==0):
return
if(y%2!=0):
oddsum(int(n/10))
print (str(y),end="")
print (" ",end="")
else:
oddsum(int(n/10))
def main():
n=int(input("Enter a value : "))
print("The odd numbers are ",end="")
oddsum(n)
s = 0
while n!=0:
y=n%10
if(y%2!=0):
s += y
n //= 10
print("The sum would be ",end=' ')
print("=",s)
return
main()
It outputs just fine, in the example it will print 1 5 and 7 as the odd numbers. However, when it calculates the sum, it just says "7" instead of 13 like it should be. I can't really understand the logic behind what I'm doing wrong. If anyone could help me out a bit I'd appreciate it :)
I understand it's an issue with the "s += y" as it's just adding the 7 basically, but I'm not sure how to grab the 3 numbers of the output and add them together.
As #Anthony mentions, your code forever stays at 156 since it is an even num.
I would suggest you directly use the string input and loop through each element.
n = input("Enter a value : ") #'1567'
sum_of_input = sum(int(i) for i in n if int(i)%2) #1+5+7=13
[print(i, end="") for i in n if int(i)%2] #prints '157'
Note that int(i)%2 will return 1 if it is odd.
1567 % 10 will return 7. You might want to add the numbers you printed in oddsum to a list, and use the sum function on that list to return the right answer.
The immediate issue is that n only changes if the remainder is odd. eg 1,567 will correctly grab 7 and then n=156. 156 is even, so s fails to increment and n fails to divide by 10, instead sitting forever at 156.
More broadly, why aren't you taking advantage of your function? You're already looping through to figure out if a number is odd. You could add a global parameter (or just keep passing it down) to increment it.
And on a even more efficient scale, you don't need recursion to do this. You could take advantage of python's abilities to do lists. Convert your number (1567) into a string ('1567') and then loop through the string characters:
total = 0
for c in '1567':
c_int = int(c)
if c_int%2!= 0:
total += c_int
print(c)
print(total)

Creating a loop and calculating the average at the end

I have an assignment as follows
Write a program that repeatedly asks the user to enter a number, either float or integer until a value -88 is entered. The program should then output the average of the numbers entered with two decimal places. Please note that -88 should not be counted as it is the value entered to terminate the loop
I have gotten the program to ask a number repeatedly and terminate the loop with -99 but I'm struggling to get it to accept integer numbers (1.1 etc) and calculate the average of the numbers entered.
the question is actually quite straightforward, i'm posting my solution. However, please show us your work as well so that we could help you better. Generally, fro beginners, you could use the Python built-in data types and functions to perform the task. And you should probably google more about list in python.
def ave_all_num():
conti = True
total = []
while conti:
n = input('Input value\n')
try:
n = float(n)
except:
raise ValueError('Enter values {} is not integer or float'.format(n))
if n == -88:
break
total.append(n)
return round(sum(total)/len(total),2)
rslt = ave_all_num()
Try the following python code. =)
flag = True
lst=[]
while(flag):
num = float(raw_input("Enter a number. "))
lst+=[num]
if(num==-88.0): flag = False
print "Average of numbers: ", round( (sum(lst[:-1])/len(lst[:-1])) , 2)
enter code hereThank you for the prompt replies. Apologies. This is the code i was working on:
`#Assignment2, Question 3
numbers=[]
while True:
num=int(input("Enter any number:"))
if num==float:
continue
if num==-88:
break
return print(" the average of the numbers entered are:",sum(numbers)/len(numbers)`

Conditional statement not working with input function

i'm just writing a simple program that should give me a random integer between 1 and 10 if i input the value of 'r'in code. here's what i have:
import sys, random
from random import *
def randomly():
return (randint(1, 10))
while True:
x = input("input string ")
x = str(x)
print (x)
if x == 'r':
print ("your generated number is", randomly())
else:
break
i know there's just something small i'm overlooking. but i can't figure out what. the randomly function works. and if i replace the 'r' conditional with an integer (i used 1) convert x with int(x) and then input 1 the program works fine. like so:
import sys, random
from random import *
def randomly():
return (randint(1, 10))
while True:
x = input("input number ")
x = int(x)
print (x)
if x == 1:
print ("your generated number is", randomly())
else:
break
for some reason the if conditional won't respond to my string. i tried x.lower() and it still won't work, while it's true that just using the method i described before with the integer would work fine. it frustrates me that my code won't work the way i want it to. for what it's worth i checked len(x) and it says 2 when i input a single character(r). i'm fairly novice to python but i have the knowledge i need, at least, to write a program like this. any help would be greatly appreciated.
i'm using visual studio 2015
python environment: python 3.2
this is my first question here. i searched to the best of my capabilities and couldn't find an answer. i apologize in advance if there is anything wrong with my question.

Simple Python Function Issue

What is wrong with this code that I'm trying to run in Python? (Pretend that indenting isn't an issue):
def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
As you can imagine, this has been pretty confusing since I entered it exactly as my textbook shows it. Thanks for the help!
You are doing two wrong things with your code.
1 :
You must not use eval with input method because eval ask for string as input while with input you are returning float value.
You can simply run your program if you are passing float as input.
x = input("Enter a number between 0 and 1: "))
You need to use raw_input(raw_input will return string data)
x = eval(raw_input("Enter a number between 0 and 1: "))
2 :with for loop you need to provide indentation.
Need to indent the function block
def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
Also, instead of eval I would just use float:
def main():
print("This program illustrates a chaotic function")
x = float(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
Sample:
>>> def main():
... print("This program illustrates a chaotic function")
... x = float(input("Enter a number between 0 and 1: "))
... for i in range(10):
... x = 3.9 * x * (1 - x)
... print(x)
...
>>> main()
This program illustrates a chaotic function
Enter a number between 0 and 1: .2
0.624
0.9150336
0.303213732397
0.823973143043
0.565661470088
0.958185428249
0.156257842027
0.514181182445
0.974215686851
0.0979659811419
I think your problem was because you run it in sublime text editor
Try running it from command line
$ python yourscript.py
and you'll see that your script run normally.
EOFError that you get was caused by sublimtext that didn't send any input to your program while the built-in input function asked for an input.

Categories

Resources