new to programming, any help would be appreciated! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: new to programming, any help would be appreciated! (/thread-29923.html) |
new to programming, any help would be appreciated! - 01andy - Sep-26-2020 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'? RE: new to programming, any help would be appreciated! - ibreeden - Sep-26-2020 Please read BBCode to learn how to post code. if i % 2 == 0: type = "Even" else: type = "Odd " print(type) RE: new to programming, any help would be appreciated! - buran - Sep-26-2020 type is built-in function, don't use it as variable name
RE: new to programming, any help would be appreciated! - 01andy - Sep-29-2020 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... RE: new to programming, any help would be appreciated! - GOTO10 - Sep-29-2020 You haven't made it very clear what you are expecting in terms of output, but a couple of things look problematic:
RE: new to programming, any help would be appreciated! - deanhystad - Sep-29-2020 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]) RE: new to programming, any help would be appreciated! - 01andy - Sep-29-2020 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? RE: new to programming, any help would be appreciated! - deanhystad - Sep-29-2020 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') RE: new to programming, any help would be appreciated! - 01andy - Oct-13-2020 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? RE: new to programming, any help would be appreciated! - deanhystad - Oct-13-2020 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) |