Create a program that asks the user for a string and then prints the string in upper case - python-3.x

EDIT: I tried the code lines in the link to other questions that were similar, however the programs did not execute correctly
I am a full-on noob trying to complete some free online resources for self improvement and learning. I am using University of Waterloo's 'Python from scratch' and CS circles course I have tried to answer this question and cannot seem to:
Write a program that asks the user for a string and then prints the string in upper case.
I have tried:
print (str(input()).upper)
AS WELL AS
text = input()
print (text.upper)
AND
print(input().upper())
all programs run, but dont have correct output so I dont know what I am missing here. It's likely obvious and I may feel foolish
I would love to learn and move on, thanks for any assistance!
this is 'Python from scratch' 2.11 problem 'g' (7th problem in set)

You were very close, the following works:
input.upper()
so, print(input.upper())
should work for you.

text=input()
print(text.upper())

print(input().upper())
This should have worked for you in Python 3.x

Related

Why is paho-mqtt ignoring my subscription

I am using the following code
client.loop_start()
client.subscribe("indentify/+")
client.subscribe("indentify/#")
client.subscribe("status/#")
#client.subscribe("#")
client.on_message=on_message
When I send identify/xxx to the server it is ignored. If I uncomment the last subscribe line I see the messages and everything else which is to be expected.
A Perl script doing essentially the same thing sees the messages as expected.
Running paho_mqtt-1.5.1-py3-none-any.whl and Python 3.8.10
On a Ubuntu 20.04.3 server.
Any ideas would be appreciated.
Jim.
It's not ignoring your subscription, it's doing exactly what you asked of it. As per a large number of computer problems, what you asked it to do is not what you wanted it to do.
Let's have a closer look:
v
|
Subscribe to indentify/+
Subscribe to indentify/#
Publish to identify/xxx
|
^
Can you see the problem now? I certainly hope so.
I suspect your "essentially the same" Perl script may have better spelling than your Python code. "Indentify" sounds like something Python coders do to fix indentation issues :-)
And, in case my answer has been lost in the rambling that is often my nature, here's the Janet and John bit:
You did not spell "identify" correctly when subscribing. There's an extraneous "n" between the first two characters ("i" and "d"), giving you "i n dentify" (sans spaces - they're just there to make it more obvious).

Python - Can't reproduce console output in a file form

I'm very new to Python, with no previous experience in programming, so if my questions seem stupid, that's because they are.
I was trying to lean a bit more about the "requests" library, so I installed it and followed the instructions in this page (http://docs.python-requests.org/en/master/user/quickstart/). When I got to "Response Content", I tried to emulate the code by copying and pasting it, creating a file named pedidos.py, and running it on my terminal. But when I run it, nothing happens.
This is the code as I copied it:
import requests
r = requests.get('https://api.github.com/events')
r.text
The strangest thing is, if I open a python console in my terminal and input this code line by line, I get a output, which doesn't happen if run it through the file.
I'm fairly certain that the answer to my question is, probably, very simple, and that I will be ashamed to have asked it once someone explains it to me. But the truth is that I'm stuck and really need some help.
Thanks in advance to anyone who takes the time to answer.

Python Assistant - Responses to variety of sentences.

I was wondering if anyone would be able to help me with a small problem. I am looking to start a project to speedup my normal time consuming processes on my computer. Within the code of my 'assistant' I would have all the functions hard coded but I wanted the flexibility of not having to type an exact sentence e.g. 'open sublime text' instead I would like to be able to talk freely and be able to say something along the lines of 'Please open sublime' or 'Can you please open sublime text'. I know that I could do something along the lines of:
if 'sublime' in text_input:
sublime()
The only problem is I have to hard code each one and if any other application have a similar word in the sentence it will perform the first if statement that is met with (if I say google sublime and the if statement for sublime text application is above the google if statement then it will open the application instead of googling it). Is there no simpler way to do this? Something more advanced or easier? Appreciate any help, new to stackoverflow and I am grateful if you could take your time to assist me with my problem.

Python won't write '♥' character to text document

I've got a bit of an annoying issue, I'm trying to write a series of json data to a text document, however, python raises a UnicodeEncodeError whenever it encounters these kinds of characters.
As per the big update with python 3, these characters print to the console just fine, its the issue when we go
with open("filename.txt", "a") as file
file.write("I ♥ ice cream")
file.close()
As I'm still a newbie to python, I haven't the slightest clue how to solve this, any ideas?
Found out how to solve this one!
First off I'd like to thank #JJJ, for hinting me down the right track, however my only criticism is that the presented solution wasn't very straight forward, and for someone with no knowledge of the significance of bytes and strings this may present quite the challenge.
Basically the problem was to do with the default method of encoding that my computer uses (the OS being the standard win 10), being cp1252.
When going into python and having the program run a simple bit of code to test this, it more clearly illustrates the issue and thus we can find a more viable solution.
text = "I ♥ IceCream"
text = text.encode("cp1252")
open('People Jobs.txt','a').write(text)
Running this in IDLE, we get this:
UnicodeEncodeError: 'charmap' codec can't encode character '\u2665' in position 2: character maps to <undefined>
Ah! Now we can see our issue! The codec can't encode the character! Knowing this we can encode the string using utf-8 before writing it to the file like so:
text = "I ♥ IceCream"
text = text.encode("utf-8")
open('People Jobs.txt','a').write(text)
Running this, we finally get:
b'I \xe2\x99\xa5 IceCream'
Which can be written to the file no worries. We can turn this back into the original message using the decode method, however for my purposes, we don't need to do that.
Once again, I'd like to extend my thanks to those who commented on my post, your extensive knowledge of the python language is quite the asset and I greatly appreciate it.
Hopefully my negligence to see these simple programming principles will benefit others when they come to laugh at this post
But hey that's why I have the name!
So until next time,
Mr Incompetent
P.S #Pratik K Thank you for the reminder of how to write this one in a more compact manner, I appreciate it :) (been doing C++ for a while so I've forgotten about python)
I tried it out, seems to work just fine. Just a side note, instead of using with ..., you can just as well write this out as: open(filename, 'a' ).write(string). This will open up the file, write/ append to it, and close it, all within one line.
Just to be clear, the syntax for your solution would be:
with open("filename.txt", 'a') as file:
file.write("I ♥ ice cream")

What exactly does "printw" do? (Ncurses)

Could someone please tell me what exactly does printw do? I tried looking for information but just could not find anything.
5 seconds on Google revealed some nice documentation.
printw() class: Print formatted output similar to printf()
and
6.3.1. printw() and mvprintw
These two functions work much like printf(). mvprintw() can be used to move
the cursor to a position and then print. If you want to move the cursor first
and then print using printw() function, use move() first and then use printw()
though I see no point why one should avoid using mvprintw(), you have the
flexibility to manipulate.
Source - NCURSES-Programming-HOWTO
Type man printw (I suppose you are not programming with Windows).

Resources