[Solved] Better way of replacing subtring from a string [duplicate]

When manipulating structures such as URLs it’s better to use tools designed for the purpose rather than treating them as strings. In this case you want to change the host (netloc) to www.site2.com So… from urllib.parse import urlparse, urlunparse new_site=”www.site2.com” url=”https://www.site1.com//vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=New” scheme, _, path, params, query, fragment = urlparse(url) new_url = urlunparse((scheme, new_site, path.replace(‘//’, “https://stackoverflow.com/”), … Read more

[Solved] How can I do this effectively? [closed]

One way to do it using sum(), list comprehension and recursion, def simulated_sum(input): “””This is a recursive function to find the simulated sum of an integer””” if len(str(input)) == 1: return input else: input_digits = [int(x) for x in str(input)] latest_sum = sum(input_digits) return simulated_sum(latest_sum) input = int(input(‘Enter a number’)) print(simulated_sum(input)) DEMO: https://rextester.com/WCBXIL71483 solved How … Read more

[Solved] How can I do this effectively? [closed]

Introduction When it comes to tackling a task, it is important to do it effectively. This can be difficult, especially if you are not sure how to go about it. Fortunately, there are many resources available to help you figure out how to do something effectively. This article will provide some tips on how to … Read more

[Solved] Using Python from a XML file ,I want to get only the tags that have a value? [closed]

Load xml, traverse it (eg recursively), return tags with non-empty text. Here as generator: import xml.etree.ElementTree as ET def getNonemptyTagsGenerator(xml): for elem in xml: yield from getNonemptyTagsGenerator(elem) if len(xml) == 0: if xml.text and xml.text.strip(): yield xml xml = ET.parse(file_path).getroot() print([elem for elem in getNonemptyTagsGenerator(xml)]) Result: [<Element ‘field’ at 0x7fb2c967fea8>, <Element ‘text’ at 0x7fb2c9679048>] You … Read more

[Solved] The function should return the string multiplied by the integer [closed]

To set a default value in a function you should do like in the code bellow and you don’t have to use str to convert x to a string, just send the string. def multiply(x,mult_int=10): return x*mult_int print(multiply(“I hope that’s not your homework.\n”, 2)) print(multiply(“Bye.”)) 1 solved The function should return the string multiplied by … Read more

[Solved] Shortening the following if statement?

As was mentioned by cco: if data.char in “123456789” data.board[row][col] = int(data.char) The in operator evaluates to true if it finds a variable in the specified sequence and false otherwise. It can be used for lists, strings, tuples and dictionaries. However the in will only check the keys of the dictionary – not the values. … Read more

[Solved] Python 3: How to get a random 4 digit number?

If you want to extend your solution to make sure the leading digit isn’t 0, there’s probably no super-concise way to write it; just be explicit: first = random.choice(range(1, 10)) leftover = set(range(10)) – {first} rest = random.sample(leftover, 3) digits = [first] + rest Notice that I’m taking advantage of the fact that sample takes … Read more

[Solved] Oops, try again. Your function failed on the message yes. It returned ‘yes’ when it should have returned ‘Shutting down’

Couple of points: Misplaced return statement. Should be at end. if yes(): It is wrong. You want to compare function input with yes. It should be if s == ‘yes’:. Same for rest also. Since you have written function definition as def shut_down(s):, it is expecting one argument. You should pass one argument while calling … Read more

[Solved] How do i print out number variables in python 3? [closed]

The main problem here is that you are calling functions as they were variables Here is your code with the functions being called correctly: import math print(‘input your height and weight to find BMI.’) num_a = int(input(“feet==”)) num_b = int(input(“inches==”)) num_c = int(input(“weight(lbs)==”)) def weight(): return int(num_c) * 2.2 def height(): return (int(num_a)) * 12 … Read more