Programming (identifier, literal and function call) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Programming (identifier, literal and function call) (/thread-12297.html) |
Programming (identifier, literal and function call) - ledangereux - Aug-18-2018 Hello Im am learning Python with Coursera. I run in to a problem. Everything was going well but now I did not receive the information I need to solve this problem. Write a program that prompts the user to enter an integer and displays that integer. The program should then prompt the user to enter the integer again and display its binary representation. For example, here are some sample program runs: Enter an integer >45 45 Enter it again >45 0b101101 You can use the built-in functions named: input, int, bin and print to help you solve this problem. Look at the documentation for the builtin-in functions at python.org. tip: Recall that you can apply a function to the result of a function as many times as you like, for example, to print the rounded value of a floating point number that is input as a string, you can use this expression: print(round(float(input('Enter a decimal number >')))) Can somebody help me explain what is going on here? thank you RE: Programming (identifier, literal and function call) - ichabod801 - Aug-18-2018 In the line of code you posted, the following things happen in the following order:
The program is suggesting you do the same sort of thing with the input, int, bin, and print function to perform the task specified. You can do the problem without it all being on one line. For example, the line they gave is equivalent to: text = input('Enter a decimal number >') number = float(text) rounded = round(number) print(rounded) RE: Programming (identifier, literal and function call) - ledangereux - Aug-19-2018 input returns a string float returns a floating point number for the string round returns the integer obtained by rounding the floating point number print displays the integer You should enter a program that contains this one example line and experiment with it before you write the program for this quiz. Experiment by removing one or more of the function calls to see how these functions work (or generate semantic errors), depending on the types of their argument objects. When you enter your program and run it, the input for one test case will be entered automatically. When your program is tested, we will always enter valid integers. input('Enter a decimal number >') float('text') Error on line 2: float('text') ValueError: could not convert string to float: 'text' Thank you so much for your reply... I start to make sense of it. But I don't think I can do it on one line. the program wants me to display it like this: For example, here are some sample program runs: Enter an integer >45 45 Enter it again >45 0b101101 I could but it reported an error again because I did not implemented the bin function on the same line I guess. What is the order to implement the function bin in this line? Still don't understand. tip: Recall that you can apply a function to the result of a function as many times as you like, for example, to print the rounded value of a floating point number that is input as a string, you can use this expression: print(round(float(input('Enter a decimal number >')))) So this is the code I write to get this result: print(round(float(input('Enter an integer >')))) int(round(float(input('Enter it again >')))) bin(45) The result is corresponding to the answer: Enter an integer >45 45 Enter it again >45 0b101101 But when I run it I get this error: Incorrect Response #TEST 1# inputs: 45 45 outputs: Enter an integer > 45 Enter it again > ** ERROR ** no line * EXPECTED * 0b101101 ---------- #TEST 2# inputs: -20 -20 outputs: Enter an integer > -20 Enter it again > ** ERROR ** no line * EXPECTED * -0b10100 ---------- #TEST 3# inputs: 256 256 outputs: Enter an integer > 256 Enter it again > ** ERROR ** no line * EXPECTED * 0b100000000 ---------- RE: Programming (identifier, literal and function call) - ichabod801 - Aug-19-2018 You are not printing the second time, and the bin is on 45, not on the user's input (you can't guarantee the input will be 45). When posting code, please use Python tags (see the BBCode link in my signature below for instructions). RE: Programming (identifier, literal and function call) - ledangereux - Aug-19-2018 thank you for your reply, print(round(float(input('Enter a decimal number >')))) print(bin(int(input('Enter it again >')))) I enter this code and I got the result I should have: Enter an integer >45 45 Enter it again >45 0b101101 And it is ok! Thank you!!!! RE: Programming (identifier, literal and function call) - gumi543 - May-05-2020 Hello! Firts of all, sorry if I make a mistake writing. I´m still learning english and I so bad :( I´m at the same course and at the same test. I have tried all the posibilities but I can´t solve it. I wrote the last answer too and it give me error. Can someone help me pls? Thanks! |