Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
johnadams123412

Help with python coding problem

Recommended Posts

Are you supposed to write these functions yourself or use built ins? The first one is a built in function ('title()').

For the second one you can just compare the lengths and return the largest one. The length of a string s is just

len(s)

 


PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Fractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 128GB Samsung NVM and a 2TB WD Blue, Gigabyte RTX 2060 Windforce OC, Seasonic Focus Plus Gold 650, Corsair Hydro H75, LG 27GL83A-B

 

Audio: FiiO E10K, Hidizs S1, FiiO FA1, Sony XB50AP

Link to post
Share on other sites

For the first one, you can use the .upper and .lower methods and concatenate it together OR use the title function.

 

For the second you can use the len function


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites
Posted · Original PosterOP
1 minute ago, martward said:

Are you supposed to write these functions yourself or use built ins? The first one is a built in function ('title()').

For the second one you can just compare the lengths and return the largest one. The length of a string s is just


len(s)

 

write these functions myself, its a practice problem im doing in a course so for long name i have this lol but i could be completely off

def long_name(name_1, name_2):

names = (name_1, name_2)

for words in names:  

 

 

Link to post
Share on other sites

The way I tackle these problems is by first starting out writing the assignment in steps and then find out how to do these steps.

Keep in mind I do not know Python, so will just write it out in C#-like 'code' (this is really just pseudocode), but it will give you some ideas to Google.

private string title(string input)
{
 	// Check if the word adheres to the standard (the capatilized stuff, possible optional)
  	// it's probably easiest now to split the word in two parts: first letter and last letter(s). make the first part uppercase, rest lowercase. see link below. 
  
 	// Now return that change input
}

private string long_name(string name1, string name2)
{
	// Compare the length of the names. Easiest to just get the length of either string and compare that, kind like this:
  	int name1Length = len(name1);
  	int name2Length = len(name2);
  	
  	// Is name 1 a higher number??
  	if(name1Length > name2Length)
    {
      return name1;
    }
  	else
    {
      return name2;
    }
}

https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/

https://www.geeksforgeeks.org/python-string-length-len/

 

You're gonna have to check yourself though how to take parameters, create functions, return stuff, how to exactly do the upper/lower case stuff.. etc.


"We're all in this together, might as well be friends" Tom, Toonami.

Sorry if my post seemed rude, that is never my intention.

"Why do we suffer a lifetime for a moment of happiness?" - Anonymous

 

Link to post
Share on other sites
2 minutes ago, IshGames said:

write these functions myself, its a practice problem im doing in a course so for long name i have this lol but i could be completely off

def long_name(name_1, name_2):

names = (name_1, name_2)

for words in names:  

 

 

Okay so I wouldn't necessarily make a tuple of the inputs, since you will always have exactly two names. You could do something like:d

def long_name(name1, name2):
  if len(name1) >= len(name2):
    return name1
  else:
    return name2

This will return name1 if it is longer or as long as name 1 otherwise it returns name2.


PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Fractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 128GB Samsung NVM and a 2TB WD Blue, Gigabyte RTX 2060 Windforce OC, Seasonic Focus Plus Gold 650, Corsair Hydro H75, LG 27GL83A-B

 

Audio: FiiO E10K, Hidizs S1, FiiO FA1, Sony XB50AP

Link to post
Share on other sites
1 minute ago, IshGames said:

write these functions myself, its a practice problem im doing in a course so for long name i have this lol but i could be completely off

def long_name(name_1, name_2):

names = (name_1, name_2)

for words in names:  

 

 

That doesn't actually do anything yet so... you're neither wrong nor right. You could just write

for name in (name1, name2):
	...

instead of creating an extra variable though.

 

With this kind of exercise it's hard to help you without doing it for you, @Slottr gave you some pretty good hints so I'd start with those.


...is there a question here? 🤔

sudo chmod -R 000 /*

What is scaling and how does it work? Asus PB287Q unboxing! Console alternatives :D Watch Netflix with Kodi on Arch Linux Sharing folders over the internet using SSH Beginner's Guide To LTT (by iamdarkyoshi)

Sauron'stm Product Scores:

Spoiler

Just a list of my personal scores for some products, in no particular order, with brief comments. I just got the idea to do them so they aren't many for now :)

Don't take these as complete reviews or final truths - they are just my personal impressions on products I may or may not have used, summed up in a couple of sentences and a rough score. All scores take into account the unit's price and time of release, heavily so, therefore don't expect absolute performance to be reflected here.

 

-Lenovo Thinkpad X220 - [8/10]

Spoiler

A durable and reliable machine that is relatively lightweight, has all the hardware it needs to never feel sluggish and has a great IPS matte screen. Downsides are mostly due to its age, most notably the screen resolution of 1366x768 and usb 2.0 ports.

 

-Apple Macbook (2015) - [Garbage -/10]

Spoiler

From my perspective, this product has no redeeming factors given its price and the competition. It is underpowered, overpriced, impractical due to its single port and is made redundant even by Apple's own iPad pro line.

 

-OnePlus X - [7/10]

Spoiler

A good phone for the price. It does everything I (and most people) need without being sluggish and has no particularly bad flaws. The lack of recent software updates and relatively barebones feature kit (most notably the lack of 5GHz wifi, biometric sensors and backlight for the capacitive buttons) prevent it from being exceptional.

 

-Microsoft Surface Book 2 - [Garbage - -/10]

Spoiler

Overpriced and rushed, offers nothing notable compared to the competition, doesn't come with an adequate charger despite the premium price. Worse than the Macbook for not even offering the small plus sides of having macOS. Buy a Razer Blade if you want high performance in a (relatively) light package.

 

-Intel Core i7 2600/k - [9/10]

Spoiler

Quite possibly Intel's best product launch ever. It had all the bleeding edge features of the time, it came with a very significant performance improvement over its predecessor and it had a soldered heatspreader, allowing for efficient cooling and great overclocking. Even the "locked" version could be overclocked through the multiplier within (quite reasonable) limits.

 

-Apple iPad Pro - [5/10]

Spoiler

A pretty good product, sunk by its price (plus the extra cost of the physical keyboard and the pencil). Buy it if you don't mind the Apple tax and are looking for a very light office machine with an excellent digitizer. Particularly good for rich students. Bad for cheap tinkerers like myself.

 

 

Link to post
Share on other sites
Posted · Original PosterOP
5 minutes ago, martward said:

Okay so I wouldn't necessarily make a tuple of the inputs, since you will always have exactly two names. You could do something like:d


def long_name(name1, name2):
  if len(name1) >= len(name2):
    return name1
  else:
    return name2

This will return name1 if it is longer or as long as name 1 otherwise it returns name2.

oh that makes it so much easier, just comparing the names and returning the name with bigger length, i dont know why i was overthinking it,
Any ideas on the capitalizing the first letter of a word thats jumbled like "JoHn" and lowercasing the rest?
im assuming youd do like .higher(0) and lower(?) idk im lost on that one

Link to post
Share on other sites
3 minutes ago, IshGames said:

oh that makes it so much easier, just comparing the names and returning the name with bigger length, i dont know why i was overthinking it,
Any ideas on the capitalizing the first letter of a word thats jumbled like "JoHn" and lowercasing the rest?
im assuming youd do like .higher(0) and lower(?) idk im lost on that one

Bruv

 

17 minutes ago, Slottr said:

For the first one, you can use the .upper and .lower methods and concatenate it together OR use the title function.

 


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites
Posted · Original PosterOP
2 minutes ago, Slottr said:

Bruv

 

 

Sorry I read your comment after I replied, I was trying to work it out, thats why i initially said .higher, then read your comment and realized im stupid and its .upper, coding is hard not gonna lie

Link to post
Share on other sites
1 minute ago, IshGames said:

Sorry I read your comment after I replied, I was trying to work it out, thats why i initially said .higher, then read your comment and realized im stupid and its .upper, coding is hard not gonna lie

lol all good

 

It'll get easier the higher up you go. All this little stuff will become natural in no time. Make sure to challenge yourself though. Don't ask for a solution, but ask for help towards a solution. It's hard, but it helps immensely 


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites
Posted · Original PosterOP
2 minutes ago, Slottr said:

lol all good

 

It'll get easier the higher up you go. All this little stuff will become natural in no time. Make sure to challenge yourself though. Don't ask for a solution, but ask for help towards a solution. It's hard, but it helps immensely 

so far I have

def title(word)

then im assuming i have to do .upper(0)  idk. and lower case rest of word? how would I go about it?

Link to post
Share on other sites
Posted · Original PosterOP
9 minutes ago, Slottr said:

lol all good

 

It'll get easier the higher up you go. All this little stuff will become natural in no time. Make sure to challenge yourself though. Don't ask for a solution, but ask for help towards a solution. It's hard, but it helps immensely 

or could i use word.title() command?

Link to post
Share on other sites
5 minutes ago, IshGames said:

so far I have

def title(word)

then im assuming i have to do .upper(0)  idk. and lower case rest of word? how would I go about it?

You can use indexing

 

So if you want everything but the first letter, you can reference it like this:

 

string[1:]

and use the .lower method on that


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites
Posted · Original PosterOP
17 minutes ago, Slottr said:

You can use indexing

 

So if you want everything but the first letter, you can reference it like this:

 


string[1:]

and use the .lower method on that

i got it thanks so much for your help

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×