some simple coding in python, don't know what to do - python

I wanted to do something like this:
for i in range(999999999999999):
print ("sending messages no." + i)
but just don't know the proper way to do it since it doesn't work like this.

The problem is, that your value i is an integer and the print-function takes a string as parameter. Therefore you need to cast your variable using the str() function.
Your code should look like this:
for i in range(999999999999999):
print ("sending messages no." + str(i))
Now, I encourage people to learn Python and so I am glad to help you out but next time, if you get a "TypeError: cannot concatenate 'str' and 'int' objects" maybe you should first check with Google (or any other place) what this error means, since it actually tells you that i is an integer and it cannot convert that to a string.
Cheers

Related

Can I use integers to use a unicode in python?

I have a slight problem that may be unsolvable in python.
Can you use an integer variable to type unicode in python? For example, let's say you have a variable: `variable = 0041`. Can you do something like (without having to manually type it) get `\u0041` (`a`)?
I have not really tried anything because nothing comes to mind when I try to solve this problem. If there is a possible solution, what is it?
Sorry if the answer is painfully obvious or I did not provide enough information, I'm new to this.
This is what the chr function is for.
>>> chr(int('0041', 16))
'A'

I received an error message that I don't quite understand

I am working on a program and I received an error message that said:
print("I will set a timer for " + shortesttime + "minutes")
TypeError: can only concatenate str (not "int") to str
I assumed it meant that I had to change the variable from an int to a string but when I tried it it didn't work. Afterwards I just thought that maybe I wasn't understanding the error message correctly.
Here's some code for context:
shortesttime = hwt.index(min(hwt))
smallesthwitem = (uhw[hwt.index(min(hwt))]) #it's finding the position of the smallest item in homeworktime and then, for example if the place of that was 2 it would find what's at the second place in uhw
print("So let's start with something easy. First you're going to do " + smallesthwitem)
print("I will set a timer for " + shortesttime + "minutes")
Sorry about the weird variable names
The error says that concatenating (with +) a string to an integer is not allowed. Other languages (BASIC comes to mind) let you do that. The best thing to do is to use a formatter. If you want a simple formatting, then all you need is:
print(f"I will set a timer for {shortesttime} minutes")
There's options in formatters to add commas for thousands and other stuff, but this is easier than mucking with type conversions. This format was introduced in python 3.6 (called f-strings). If you are between 3.0 and 3.5 use
print("I will set a timer for {} minutes".format(shortesttime))
Which is equivalent, just a bit longer and not as clear.
Always Remember : To join multiple strings, You perhaps have to strings only. For example, You can't concatenate int with str .
So to print it you have to convert it to a string which is known as str in python world.
On the 4th line change it to : print("I will set a timer for " + str(shortesttime) + "minutes")
One more way would be formatted string :
Like this, print(f"I will set a timer for {shortesttime} minutes"). Formatted strings automatically converts any datatype to string.
As #ferdbugs mentioned earlier, you can cast any value to a string type.
Your code should look somewhat like this
shortesttime = hwt.index(min(hwt))
smallesthwitem = (uhw[hwt.index(min(hwt))]) #it's finding the position of the smallest item in homeworktime and then, for example if the place of that was 2 it would find what's at the second place in uhw
print("So let's start with something easy. First you're going to do " + smallesthwitem)
print("I will set a timer for " + str(shortesttime) + "minutes")
Hope this helps! Do let me know in the comments if you still get the same error or a different error.
Try:
print("I will set a timer for " + str(shortesttime) + "minutes")
You can cast it to a string.

Newbie at Python, i was trying to create a calculator just for fun, and this error showed up

I'm new to coding and I'm using Python. And I was making a simple calculator, just to mess around with the IDLE, and I keep getting this error:
"ValueError: could not convert string to float: 'bread'"
Here is the code I have written.
total = float("bread") + float("coffee") + float("milk")
I was hoping someone could point me to what is wrong here.
You have to input the actual variable, not the string of the variable. If you put anything between "", it automatically becomes a string.
Try this:
total = float(bread) + float(coffee) + float(milk)
It will work.
The reason why your program didn't work is, that you can't calculate the float value of a string. Imagine sitting in a maths class and your teacher tells you to calculate what bread plus coffee is.
Because you put the words between "", the compiler interpreted them as strings and not as your variables.

Understing how to search for specific string using find() function in python

I'm new to python, I study it by myself, and I just want to make sure I understand something about find() function...
If I want to find a specific string, it will find the first index of the first letter/number of the string or the the first time this strings?
for exaple, there's the next given string:
"I$want$moeny$and$food"
If I type find('$')
it will give me the first time '$' showed up in this string, which is 1.
However, if I want to find "$moeny", I will get the the number 6, means python serched where the full "$moeny" string is showed in the first time?
and If I type find('$mommy') it will give me an Error, and not index 1.
Am I right? Did I understand it clearly?
Thanks a lot :)
The best answer is to try it yourself and see what happens.
A good practice is to enter the CMD or shell and type "python" and try various commands.

Why use str(id)?

My SDK comes with code appearing with rows like this
id=str(profile["id"])
It makes me wonder why something like the following shouldn't work
id=profile["id"]
Casting I believe is expensive so either the same type can be used or polymorphism at the method called. Can you tell why I must cast the id to a string?
Thank you
There is no casting in Python. Str(67) does not cast. It calls the __str__ method on the integer object, which generates a string representation of itself.
This is necessary to make sure that profile['id'] is a string.
It turns profile[id] into a string, python doesn't do this automatically, and further along in the code, the program probably checks profile[id] against a string. Without this conversion, you would get a typeerror: Trying to compare a string with an integer.
Python does not do arbitrary run time type conversion. You can't use an integer as a string.
It turns profile[id] into a string

Categories

Resources