print("\nNumber\tType\tSquares\tRoots") print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") for i in range(start,end,step): sq =i**2 root =math.sqrt(i) num =(i%2) print(i, '\t', num, '\t', sq, '\t', root) print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ")I am having a hell of a time trying to get the 'num' to display 'Even' or 'Odd'. I have it to the point where it will display '0' for even and '1' for odd but how am I suppose to assign it to the 'num' as 'Even' or 'Odd'?
new to programming, any help would be appreciated!
new to programming, any help would be appreciated!
|
Sep-26-2020, 08:38 AM
Please read BBCode to learn how to post code.
if i % 2 == 0: type = "Even" else: type = "Odd " print(type)
Sep-26-2020, 09:07 AM
type is built-in function, don't use it as variable name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs import math start= int(input("please enter starting number:")) end= int(input("please enter upper limit number:")) step= int(input("please enter step number:")) while start > end and end-start>step: start= int(input("please enter starting number:")) end= int(input("please enter upper limit number:")) step= int(input("please enter step number:")) print("\nNumber\tType\tSquare\tRoot") print("_________________________________") for i in range(start,end,step): sq =i**2 root =math.sqrt(i) break if i % 2 ==0: type = "Even" else: type = "Odd" print(i, '\t', type, '\t', sq, '\t', root) print("_________________________________________")SO input type but the program now runs a single line as opposed to the user inputted numbers...
Sep-29-2020, 01:54 AM
You haven't made it very clear what you are expecting in terms of output, but a couple of things look problematic:
Sep-29-2020, 03:11 AM
If I wanted to display Even/Odd for 0/1 I would do it like this
evenodd = ['Even', 'Odd'] for i in range(11): print(i, 'is', evenodd[i%2])
import math
start= int(input("please enter starting number:")) end= int(input("please enter upper limit number:")) step= int(input("please enter step number:")) while start > end and end-start>step: start= int(input("please enter starting number:")) end= int(input("please enter upper limit number:")) step= int(input("please enter step number:")) print("\nNumber\tType\tSquare\tRoot") print("_________________________________") for i in range(start,end,step): sq =i**2 root =math.sqrt(i) if i % 2 ==0: type = "Even" else: type = "Odd" print(i, '\t', type, '\t', sq, '\t', root) print("_________________________________________") OUTPUT IS please enter starting number:1 please enter upper limit number:13 please enter step number:1 Number Type Square Root _________________________________ 12 Even 144 3.4641016151377544 _________________________________________ >>> but it should display Numbers 1-13 for some reason it stopped displying those numbers in the table?
Sep-29-2020, 04:40 PM
(This post was last modified: Sep-29-2020, 05:32 PM by deanhystad.)
Tough to say since I cannot see how the code is indented. This code works for me:
import math start= int(input("please enter starting number:")) end= int(input("please enter upper limit number:")) step= int(input("please enter step number:")) while start > end and end-start>step: start= int(input("please enter starting number:")) end= int(input("please enter upper limit number:")) step= int(input("please enter step number:")) print("\nNumber\tType\tSquare\tRoot") print("_________________________________") for i in range(start,end,step): sq =i**2 root =math.sqrt(i) if i % 2 ==0: type = "Even" else: type = "Odd" print(i, '\t', type, '\t', sq, '\t', root) print("_________________________________________") 13 is not displayed because range stops before the end value. To include 13 in your calculations your loop should be:for i in range(start, end+1, step):I think the reason for this is the most common way to use range is: for i in range(13):This will loop 13 times starting with i = 0 and ending with i = 12. The table will line up better if you use this print: print(i, type, sq, root, sep = '\t')
Oct-13-2020, 06:20 PM
def find_min(a,b,c):
min(a,b,c) def find_max(a,b,c): max(a,b,c) def main(): a = int(input("Enter number 1:")) b = int(input("Enter number 2:")) c = int(input("Enter number 3:")) find_min(a,b,c) find_max(a,b,c) print("The min is",{find_min}) print("The max is",{find_max}) main() user input is: Enter number 1:5 Enter number 2:6 Enter number 3:7 and the output I get is: The min is {<function find_min at 0x10e618b80>} The max is {<function find_max at 0x1100591f0>} I don't understand why im getting that and not the minimum value of 5 and the maximum value of 7?
Oct-13-2020, 07:10 PM
Your functions do not return a value. That is why you are getting back a function object instead of a number. Calling max(a, b, c) does not make find_max(a, b, c) return the result. Your function needs something like: return max(a, b, c)
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
New to Python - Not sure why this code isn't working - Any help appreciated | TheGreatNinx | 4 | 2,337 |
Jul-22-2023, 10:21 PM Last Post: Pedroski55 |
|
Any help is appreciated! | Butch12 | 3 | 1,619 |
Jul-22-2023, 03:23 AM Last Post: Larz60+ |
|
interloper help appreciated combining two python3 scripts to add new conditions. | Ricktoddfrank | 0 | 1,962 |
Jun-22-2021, 10:24 PM Last Post: Ricktoddfrank |
Users browsing this thread: 1 Guest(s)